0

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
   }
 });
Himanshu
  • 4,327
  • 16
  • 31
  • 39
dev.knockout
  • 319
  • 2
  • 5
  • 11
  • possible duplicate of [Catch errors in jquery ajax error callback?](http://stackoverflow.com/questions/10589758/catch-errors-in-jquery-ajax-error-callback) – Vikrant Apr 17 '15 at 04:18
  • possible duplicate of [jQuery Ajax error handling, show custom exception messages](http://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages) –  Apr 17 '15 at 04:18
  • 1
    You are passing the data response to the callback. But your callback function expects two arguments. So in your success function you should be doing this: callback(data,{success: true}); basd on your implementation. And for error: callback(response,{error: true }); – Hoyen Apr 17 '15 at 04:24

1 Answers1

1

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});}
 });
}