atelion
atelion

Reputation: 71

Cannot bind function to ajax request

I'm trying to append the function ajaxFailureHandler to the ajax request:

 $.ajax({
    url: GlobalVariables.baseUrl + '/dashboard_api/ajax_save_user',
    dataType: 'text',
    cache: false,
    contentType: false,
    processData: false,
    data: form_data,
    type: 'post',
    success: function (response) {

      alert(true);
    }.bind(this),
    error: function (response) {
        alert(false);
    }
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);

but I actually get this error:

Uncaught TypeError: {(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)}.bind is not a function

what can I do for fix this?

Upvotes: 0

Views: 175

Answers (2)

charlietfl
charlietfl

Reputation: 171690

You can use context option to change this in all the callbacks.

You don't need error if you use .fail()

Set dataType:'json' if you expect json response

$.ajax({

    context: this,// binds `this` in success and fail callbacks
    dataType: 'json',    

    url: GlobalVariables.baseUrl + '/dashboard_api/ajax_save_user',
    cache: false,
    contentType: false,
    processData: false,
    data: form_data,
    type: 'post',
    success: function (response) {
      alert(true);
    }
}).fail(GeneralFunctions.ajaxFailureHandler);

Upvotes: 0

raphaelSeguin
raphaelSeguin

Reputation: 434

You've bound the settings object you're passing to $.ajax(). I think you probably meant to bind the error function like this :

$.ajax({
    url: GlobalVariables.baseUrl + '/dashboard_api/ajax_save_user',
    dataType: 'text',
    cache: false,
    contentType: false,
    processData: false,
    data: form_data,
    type: 'post',
    success: function (response) {
      alert(true);
    }.bind(this),
    error: function (response) {
        alert(false);
    }.bind(this)
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);

Upvotes: 1

Related Questions