jQuery Ajax 404处理
我试图访问一个404事件,我可以看到回来作为404与firebug,但错误function没有踢,用我的下面的代码我总是得到错误:成功?
即。
$.ajax({ type: 'get', url: 'url: 'http://sofzh.miximages.com/jquery/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); } });
我再次知道1000%,我在firebug中获得404 Not Found,它永远不会触发错误。
我错过了什么吗?
跨站点请求,JSONP,不会触发错误调用。 我自己也有同样的问题: http : //forum.jquery.com/topic/jquery-ajax-with-datatype-jsonp-will-not-use-error-callback-if-request-fails
您还可以使用$ .ajaxError函数,就像这样
$("#message").ajaxError(function(event, request, settings){ $(this).show(); $(this).append("Error requesting page " + settings.url + " "); });
我有一个非常相似的结果/问题。 底线是我未能确保我的URL在ajax函数的数据部分中不包含任何相同的param名称 。
我的原因是,我用PHP代码生成了URL以获取URL
pines_url('com_referral', 'sendhttprequest')
使用参数输出到url
option=com_referral
和一个参数
action=sendhttprequest
我的大问题是我还试图在AJAX函数的数据部分发送一个参数“动作”。
data: {"action": "getpoints"}
所以第二个动作是覆盖它“getpoints”不存在,但棘手的部分是firebug显然不会告诉我带有getpoint的url,它会告诉我url为sendhttprequest。
希望这不会令人困惑,但我也希望这可以帮助其他有类似问题的人!
如果在指定时间内没有响应,则添加超时值将导致错误回调运行。 回调为5000,如果jsonp请求在5秒内没有响应(即404s),则错误回调将运行。
$.ajax({ type: 'get', timeout: 5000, url: 'url: 'http://sofzh.miximages.com/jquery/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); } });
我.fail
知道为什么,但如果您使用链接.fail
方法,即使它是一个不同的站点,也会正确捕获错误:
$.ajax({ type: 'get', url: 'http://example.com', success: function(data, textStatus, XMLHttpRequest){ console.log('Error: ' + textStatus); } }).fail(function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(xhr.statusText); alert(xhr.responseText); });