Handling a 404 request in getJSON () jquery

I have the following code and I get JSON from the site using JSONP. I want to process an error code, for example, 404 bad request. The following does not work 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"); }); 

Successful and complete methods work, but the error method does not work.

+4
source share
3 answers

Try fail instead of error (see Delayed Dock ).

+5
source
 $.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"); }); 

The above code may help you.

+4
source

There is an answer in this thread: fooobar.com/questions/1458263 / ...

Since you are using JSONP, the callback generated by jQuery is not called, so the failure callback is never called. This is normal behavior for JSONP, alas.

+1
source

All Articles