最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check for 404 error status in jquery - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 3

From 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(){
    }
  }
});
发布评论

评论列表(0)

  1. 暂无评论