jQuery Ajax 404处理

Lee*_*Lee 25 jquery

我试图访问一个404事件,我可以看到回来作为404与firebug,但错误功能没有踢,用我的下面的代码我总是得到错误:成功?

即.

$.ajax({
    type: 'get',
    url: 'url: 'https://admin.instantservice.com/resources/smartbutton/5702/10945/available.gif?' + Math.floor(Math.random()*10001),
    success: function(data, textStatus, XMLHttpRequest){
        console.log('Error: ' + textStatus);
    },
    error:function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(xhr.statusText);
        alert(xhr.responseText);
    }
});
Run Code Online (Sandbox Code Playgroud)

我再次知道1000%,我在firebug中获得404 Not Found,它永远不会触发错误.

我错过了什么吗?

fir*_*all 24

跨站点请求,JSONP,不会触发错误调用.我自己也遇到了同样的问题:http://forum.jquery.com/topic/jquery-ajax-with-datatype-jsonp-will-not-use-error-callback-if-request-fails

  • 那么非jsonp请求呢,问题中没有发现? (2认同)

Fer*_*ran 13

您还可以使用$ .ajaxError函数,就像这样

$("#message").ajaxError(function (event, request, settings) {
    $(this).show();
    $(this).append("<li>Error requesting page " + settings.url + "</li>");
});
Run Code Online (Sandbox Code Playgroud)

  • 这对我没有帮助的原因是因为在JSONP的情况下它仍然没有被调用.来自http://api.jquery.com/ajaxError/:"注意:不会为跨域脚本和跨域JSONP请求调用此处理程序." (7认同)
  • 不知道为什么这没有更多的upvotes,工作完美见:http://api.jquery.com/ajaxError/ (3认同)