I use this code to get some information from twitter via their api:
$.ajax({
url : apiUrl,
cache : false,
crossDomain: true,
dataType: "jsonp",
success : function(html) {
// ...
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
}
});
However, if the apiUrl variable provides a correct url, this code work fine, e.i. the success object is executed, but if the url isn't correct, e.i. 404 error is returned from twitter, the error object is never executed. It doesn't log anything in console. How should I check for 404 error status in this case?
I use this code to get some information from twitter via their api:
$.ajax({
url : apiUrl,
cache : false,
crossDomain: true,
dataType: "jsonp",
success : function(html) {
// ...
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
}
});
However, if the apiUrl variable provides a correct url, this code work fine, e.i. the success object is executed, but if the url isn't correct, e.i. 404 error is returned from twitter, the error object is never executed. It doesn't log anything in console. How should I check for 404 error status in this case?
Share Improve this question asked Mar 2, 2012 at 13:49 King JulienKing Julien 11.4k24 gold badges95 silver badges132 bronze badges 3- This might solve the problem: stackoverflow./questions/4281274/jquery-ajax-404-handling – Smamatti Commented Mar 2, 2012 at 13:51
- 1 give an example of an incorrect url – JamesHalsall Commented Mar 2, 2012 at 13:52
- stackoverflow./a/4803044/137972 might be relevant. – Christoffer Commented Mar 2, 2012 at 13:54
2 Answers
Reset to default 3From jQuery API ajax docs:
error option
Note: This handler is not called for cross-domain script and JSONP requests.
http://api.jquery./jQuery.ajax/
According to the docs, use statusCode
setting in .ajax
.
$.ajax({
...
statusCode: {
404: function(){
}
}
});