我正在使用 ajax 请求(POST 方法)提交表单,并检查响应上的 HTTP 状态代码以查看它是否成功。
它在 Firefox 上运行良好,但在 MSIE-8 上当然不行。提交实际上工作正常,我可以检查我的服务器并确认提交有效并且服务器以状态代码 204 进行响应。同样,firefox 正确地从请求对象中为我提供了状态代码 204,但 IE 给出了状态代码1223.
有什么想法可以在 MSIE 中获取准确的状态代码吗?提交表单并检查响应的代码如下。
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。
这是一个已知的错误,大多数基于 javascript 的框架都会处理这种情况,并将 IE 中的 1223 标准化为 204
所以你的问题的解决方案是这样的
// 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 http://trac.dojotoolkit.org/ticket/2418
原型 -https://prototype.lighthouseapp.com/projects/8886/tickets/129-ie-mangles-http-response-status-code-204-to-1223 https://prototype.lighthouseapp.com/projects/8886/tickets/129-ie-mangles-http-response-status-code-204-to-1223
YUI - http://developer.yahoo.com/yui/docs/connection.js.html http://developer.yahoo.com/yui/docs/connection.js.html(处理事务响应)
jQuery -http://bugs.jquery.com/ticket/1450 http://bugs.jquery.com/ticket/1450
ExtJS - http://www.sencha.com/forum/showthread.php?85908-FIXED-732-Ext-doesn-t-normalize-IE-s-crazy-HTTP-status-code-1223 http://www.sencha.com/forum/showthread.php?85908-FIXED-732-Ext-doesn-t-normalize-IE-s-crazy-HTTP-status-code-1223
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)