Jquery ajax 错误回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3642348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery ajax error callback
提问by simplyharsh
I need some suggestions here or maybe some explanations. I have a jquery ajax call,
我需要一些建议或一些解释。我有一个 jquery ajax 调用,
$.ajax({
type: "GET",
url: base_url+'/ajax/fetch/counts/',
dataType: 'json',
data: {},
error: function(xhr, error){
console.debug(xhr); console.debug(error);
},
success: display_counts
});
It's working fine. My success
callback fires correctly with response. But, what I noticed is that my error
callback is fired every time, even when my call returns success status 200. In the above error
callback, I see that object xhr.status
is 200.
它工作正常。我的success
回调正确触发响应。但是,我注意到error
每次都会触发我的回调,即使我的调用返回成功状态 200。在上面的error
回调中,我看到该对象xhr.status
是 200。
Can anybody explain what's wrong, or what is happening here? error
callback is supposed to fire only when I have 404 or maybe a non-200 response. Are my assumptions correct?
任何人都可以解释什么是错的,或者这里发生了什么?error
只有当我有 404 或非 200 响应时才应该触发回调。我的假设正确吗?
Thanks.
谢谢。
采纳答案by eis
Error callback is called on http errors, but also if JSON parsing on the response fails. This is what's probably happening if response code is 200 but you still are thrown to error callback.
错误回调会在 http 错误时调用,但也会在响应上的 JSON 解析失败时调用。如果响应代码为 200 但您仍然被抛出错误回调,这可能会发生这种情况。
回答by pala?н
Just an suggestion, try using the $.ajaxSetup()to get the correct error like this:
只是一个建议,尝试使用$.ajaxSetup()来获得正确的错误,如下所示:
$(function() {
$.ajaxSetup({
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
回答by Chris Laplante
A few things I can think of:
我能想到的几点:
- Make sure you have disabled caching by setting
cache: false
. - If you are using Firefox, try using Firebug and the Net tab to monitor the request
- Don't rely on the browser's JSON parser. I would recommend this one: https://github.com/douglascrockford/JSON-js/blob/master/json2.jsfrom the creator of JSON no less
- 确保您已通过设置禁用缓存
cache: false
。 - 如果您使用的是 Firefox,请尝试使用 Firebug 和 Net 选项卡来监视请求
- 不要依赖浏览器的 JSON 解析器。我会推荐这个:https: //github.com/douglascrockford/JSON-js/blob/master/json2.js来自 JSON 的创建者不少于
回答by Josh
I'm not a jQuery expert, but I know that bwith Prototype.js, the AJAX error handler fires if the request is successful but the success
handler causes an an error. Is that the same in jQuery? You could test if this is what's happening by putting the entire contents of display_counts
in a try..catch block.
我不是 jQuery 专家,但我知道在 Prototype.js 中,如果请求成功,AJAX 错误处理程序将触发,但success
处理程序导致错误。这在jQuery中是一样的吗?您可以通过将 的全部内容display_counts
放入try..catch 块中来测试是否发生了这种情况。
回答by aularon
A recent questionhad similar problem with json
jquery requests, try removing surrounding ()
from your json response.
一个最近的问题有类似的问题json
jQuery的要求,尝试删除周围()
从您的JSON响应。
回答by jonasdev
Change dataType
from plain/text
to html
变化dataType
来自plain/text
于html