gerd
9/26/2016 - 3:25 PM

A reusable generic ajax post method. Pass in a URL, data set, target div and callback function to run on success.

A reusable generic ajax post method. Pass in a URL, data set, target div and callback function to run on success.

    /**
     * Method to post the ajax query in addition to showing the ajax indicator
     * on target div
     * @method ajaxPostJSON
     * @param targetDiv -
     *            the parentDiv into which the data will be loaded
     * @param url -
     *            the AJAX URL to be invoked
     * @param arrParams -
     *            pass an array of parameters
     * @param funcCallBack -
     *            the call back method which will be called on success of the
     *            ajax
     * @returns
     */
    ajaxPostJSON : function(targetDiv, url, dataVal, funcCallBack, callBackScope) {
        // Iterate over the params and add to the data
        $.ajax({
            'url' : url,
            'contentType' : 'application/json',
            'dataType' : 'json',
            'method' : 'post',
            // This parameter will override the global
            'global' : false,
            'beforeSend' : function(xhr) {
                // hide all the children and display background
            	xhr.setRequestHeader('X-TransactionID', LN.TRANSACTION_ID);
                LN.showAjaxIndicator(targetDiv);
            },
            'data' : dataVal,
            'success' : function(data, textStatus, jqXHR) {
                funcCallBack(callBackScope, data, textStatus);
            },
            'error' : function(jqXHR, textStatus, errorThrown) {
                LN.showError(targetDiv);
            },
            'complete' : function(jqXHR, textStatus) {
                // Show all the children when either a success or an error.
                LN.hideAjaxIndicator(targetDiv);
                if (callBackScope != null && callBackScope.callbackComplete != null && typeof(callBackScope.callbackComplete) == 'function'){
                	callBackScope.callbackComplete(jqXHR, textStatus);
                }
            }
        });
    }