简体   繁体   English

jQuery $ .get / $。ajax传递HTTP状态代码200而不是预期的状态代码201或202

[英]jQuery $.get/$.ajax passes HTTP status code 200 instead of expected status code of 201 or 202

I've a server that returns HTTP status code 200, 201, and 202 from the same url. 我有一台服务器,从同一URL返回HTTP状态代码200、201和202。 In Chrome, I've confirmed with the Network debugging panel that the Status Code is what I expect it to be (ie 200, 201 or 202). 在Chrome浏览器中,我已经通过“网络”调试面板确认了状态代码是否符合我的预期(即200、201或202)。 I rely on that status code to determine the next step. 我依靠该状态码来确定下一步。

I'd expect that the callbacks for jQuery (version 1.5.2) AJAX requests to set jqxhr.status to the status code that the server sends. 我希望jQuery(1.5.2版)AJAX的回调请求将jqxhr.status设置为服务器发送的状态代码。 However, the status code is always 200, even if the code sent by the server is 201 or 202. 但是,即使服务器发送的代码是201或202,状态代码也始终为200。

In other words, the following code prints Code: 200 regardless of what the server sends. 换句话说,无论服务器发送什么内容,以下代码都会显示Code: 200

$.get(url, {}, function (data, textStatus, xhr ) {
    alert("Code: " + xhr.status);
});

Why is this happening, and more importantly, how can one get the actual status code in a jQuery AJAX callback for $.get or $.ajax ? 为什么会发生这种情况,更重要的是,如何在jQuery AJAX回调中获取$.get$.ajax的实际状态代码?

Thank you for reading. 感谢您的阅读。

From what I have experienced jQuery is just not set up very well for handling actual status codes in the response. 根据我的经验,jQuery不能很好地设置响应中处理实际状态代码的能力。 You can try just doing a manual AJAX call using some good old bare bones JS and handle the status yourself. 您可以尝试使用一些好的旧的裸露JS手动进行AJAX调用,然后自行处理状态。

Here are a few tutorials on how to do so. 以下是一些有关如何执行此操作的教程。

http://www.degraeve.com/reference/simple-ajax-example.php http://www.degraeve.com/reference/simple-ajax-example.php

http://www.w3schools.com/ajax/default.asp http://www.w3schools.com/ajax/default.asp

request.status is where you should be able to access the status code in your request object. request.status是您应该能够访问请求对象中的状态代码的位置。 Here is another page showing a little bit about how to access even more granular information about the status of the request. 这是另一个页面,显示了有关如何访问有关请求状态的更详细信息的一些信息。

http://www.ibm.com/developerworks/web/library/wa-ajaxintro3/ http://www.ibm.com/developerworks/web/library/wa-ajaxintro3/

Hope that helps you nail it! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM