Standard JQuery-Ajax Code Template (v.2)

$.ajax({
    type: 'POST',
    url: '../../../../AreaName(If any)/ControllerName/MethodName/',
    //If datatype is of FormCollection, following 3 lines are not needed-
    //====================================================
    //contentType: 'application/json; charset=utf-8',
    //data: "{'agentdata':'"+ agentdata +"'}",
    //OR, data:JSON.stringify({ key1: value1, key2: value2}),
    //dataType: 'json',
    //====================================================
    //If datatype is of FormCollection, following 3 lines are needed-
    //====================================================
    //processData: false,  // tell jQuery not to process the data
    //contentType: false,   // tell jQuery not to set contentType    
    //data: Data,
    //====================================================
    beforeSend: function () {
        $('SubmitButton').attr('disabled', 'disabled');
        $('SubmitButton').html('Working…');
    },
    success: function (result) {
        if (result == 'Success') {
            SuccessfullSaveNotification();
            ResetMainForm(MainForm);
        }
        else {
            ShowRegularError(result);
        }
    },
    error: function (request, status, error) {
        ShowRegularError('Failed to save. Error Details – Request: ' + request + ', Status: ' + status + ', Error: ' + error);
    },
    complete: function () {
        $('SubmitButton').removeAttr('disabled');
        $('SubmitButton').html('Save changes');
    }
});

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s