MSIE 为 Ajax 请求返回状态码 1223 [英] MSIE Returns status code of 1223 for Ajax Request
问题描述
我正在使用 ajax 请求(POST 方法)提交表单,并检查响应中的 HTTP 状态代码以查看它是否成功.
I'm submitting a form using an ajax request (POST method), and checking the HTTP status code on the response to see if it was successful or not.
它在 Firefox 上运行良好,但在 MSIE-8 上当然不行.提交实际上工作正常,我可以检查我的服务器并确认提交工作并且服务器以 204 的状态代码响应.同样,firefox 正确地从请求对象中给了我 204 的状态代码,但 IE 给出了一个状态代码1223.
It works fine on Firefox, but of course doesn't on MSIE-8. The submission actually works fine, I can check my server and confirm that the submission worked and the server responded with a status code of 204. Again, firefox correctly gives me the status code of 204 from the request object, but IE gives a status code of 1223.
任何想法如何在 MSIE 中获得准确的状态代码?提交表单并检查响应的代码如下.
Any ideas how I can get an accurate status code in MSIE? The code that submits the form and checks the response is below.
var req = new XMLHttpRequest();
req.open("POST", "p.php?i=" + self.__isid, true);
//Send the proper header information along with the request
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", formdata.length);
req.setRequestHeader("Connection", "close");
req.onreadystatechange = function()
{
if(req.readyState == 4)
{
if(req.status == 204 || req.status == 200)
{
//Success. Update the feed.
self.__postFeed.update();
self.__form.reset();
}
else
{
//TODO: Better error handling.
alert("Error submitting post:\n" + req.responseText);
}
}
}
req.send(formdata);
推荐答案
MSXML HTTP 中的 XMLHTTPRequest 实现(在至少在 Windows XP SP3+ 上的 IE 8.0 中)不处理 HTTP 响应状态码 204(无内容)正确;`status' 属性有值 1223.
XMLHTTPRequest implementation in MSXML HTTP (at least in IE 8.0 on Windows XP SP3+) does not handle HTTP responses with status code 204 (No Content) properly; the `status' property has the value 1223.
这是一个已知的错误,大多数基于 javascript 的框架都会处理这种情况并将 IE 中的 1223 规范化为 204
This is a known bug and most of the javascript based frameworks handle this situation and normalizes 1223 to 204 in IE
所以你的问题的解决方案是这样的
So the solution to your problem would be like this
// Normalize IE's response to HTTP 204 when Win error 1223.
status : (conn.status == 1223) ? 204 : conn.status,
// Normalize IE's statusText to "No Content" instead of "Unknown".
statusText : (conn.status == 1223) ? "No Content" : conn.statusText
参考:
dojo - http://trac.dojotoolkit.org/ticket/2418
YUI - http://developer.yahoo.com/yui/docs/connection.js.html (handleTransactionResponse)
YUI - http://developer.yahoo.com/yui/docs/connection.js.html (handleTransactionResponse)
JQuery - http://bugs.jquery.com/ticket/1450
这篇关于MSIE 为 Ajax 请求返回状态码 1223的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!