1

I'm trying to make the AJAX call bark and squick at me so I'm executing a totally wrong URL. For some reason, none of the methods that are supposed to do the barking och squicking is invoked.

What is the reason?

$.ajax({
  url: "http://totally.wrong.url",
  dataType: 'jsonp',
  error: function () { alert("bark 1"); },
  failure: function () { alert("bark 2"); }
})
  .fail(function () { alert("squick"); })
  .done(function () { alert("woof"); });

Of course, if I change the URL to point to the correct WCF service, I get an approving woof.

According to jQuery API, I should use fail and done syntax but I'll be fine either way.

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

3 Answers3

3

Ajax with JSONP and Cross Site Requests won't trigger the error callback.

Look at the accepted answer in this question: jQuery Ajax 404 Handling

Hopefully this should explain what you need to make it work.

Community
  • 1
  • 1
fabfry
  • 140
  • 4
2

I don't know if failure is supposed to work but I always use "error" and "success" instead. So try:

   $.ajax({
      url: "http://totally.wrong.url",
      dataType: 'jsonp',
      error: function () { alert("bark");},
      success: function () { alert("woof");} 
      }
    })
1
$.ajax({
  url: "http://totally.wrong.url",
  dataType: 'jsonp',
  **error**: function () { alert("bark"); }
})
  .fail(function () { alert("squick"); })
  .done(function () { alert("woof"); });

error is the request fail callback function and not failure

redDevil
  • 1,909
  • 17
  • 25