MSIE为Ajax请求返回状态代码1223

时间:2012-04-06 14:25:24

标签: javascript ajax internet-explorer xmlhttprequest

我正在使用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);

1 个答案:

答案 0 :(得分:34)

  

MSXML HTTP中的XMLHTTPRequest实现(at   至少在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

<强>参考:

道场 - http://trac.dojotoolkit.org/ticket/2418

原型 - 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(handleTransactionResponse)

JQuery - 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

相关问题