I have the following code and I am retrieving JSON from a site using JSONP. I want to handle error code like 404 bad request. Following is not working for me.
$.getJSON('https://xyz.com/search?jsonp-callback=?', function(data) {
alert("success");
})
.success(function() { alert("success 2"); })
.error(function() { alert("error occurred "); })
.complete(function() { alert("Done"); });
Success and complete methods are working but error method is not working.
Try fail
instead of error
(see the Deferred doc).
$.getJSON('https://xyz.com/search?jsonp-callback=?', function(data) {
alert("success");
})
.success(function() { alert("success 2"); })
.error(function(event, jqxhr, exception) {
if (jqxhr.status == 404) {
alert("error occurred ");
}
})
.complete(function() { alert("Done"); });
Above code may help you.
This thread has the answer : https://stackoverflow.com/a/14371049/685925
Since you're using JSONP, the callback created by jQuery isn't called so the failure callback never gets called. That's the normal behavior for JSONP, alas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With