dev.knockout
dev.knockout

Reputation: 399

How to catch error in ajax callback between success and error?

I have a method call Ajax:

function callAjax(url, type, json, callback) {
  $.ajax({
    type: type,
    url: url,
    data: json,
    async: false,
    success: function (data) {
        if (callback) {
            callback(data);
        }
    }
});
}

But I don't call back with error in callback.
For example, I can callAjax method like this:

 callAjax("http://stackoverflow.com/", "POST", data, function(resp,status){
  if(status.success){
      //code here
    }
     if(status.error){
   //code here
   }
 });

Upvotes: 0

Views: 580

Answers (1)

user4790427
user4790427

Reputation:

this will work

function callAjax(url, type, json, callback) {
  $.ajax({
    type: type,
    url: url,
    data: json,
    async: false,
    success: function (data) {
        if (callback) {
            callback(data,{"success":true});
        }
    },
    error: function(){callback(null,{"error":true});}
 });
}

Upvotes: 1

Related Questions