jQuery的阿贾克斯错误回调 [英] Jquery ajax error callback

查看:136
本文介绍了jQuery的阿贾克斯错误回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在这里提出一些建议或者是一些解释。我有一个jquery Ajax调用,

I need some suggestions here or maybe some explanations. I have a jquery ajax call,

$.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
});

它的正常工作。我的成功回调火灾与反应正常。但是,我注意到的是,我的错误回调每次发射,甚至当我调用返回成功状态200。在上述错误回调,我看到该对象 xhr.status 200

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.

有人能解释什么是错的,还是这里发生了什么? 错误的回调应该是当我有404或者一个非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?

感谢。

推荐答案

错误回调呼吁HTTP错误,而且还的如果在响应JSON解析失败的。这是什么可能发生,如果响应code是200,但你仍然抛出错误回调。

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.

这篇关于jQuery的阿贾克斯错误回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆