2012-04-06 83 views
14

我使用ajax请求(POST方法)提交表单,并检查响应中的HTTP状态代码以查看它是否成功。MSIE为Ajax请求返回状态码1223

它在Firefox上正常工作,但当然不在MSIE-8上。提交实际上工作正常,我可以检查我的服务器并确认提交工作正常,服务器响应的状态码为204.同样,firefox正确地向我提供了请求对象的状态码204,但IE给出了状态码1223.

任何想法如何在MSIE中获得准确的状态码?下面是提交表单并检查响应的代码。 (在Windows XP SP3 +在 至少IE 8.0)在MSXML HTTP

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); 
+0

确实有点多的实验:如果我有服务器返回的状态代码200,那么MSIE工作正常。但是,如果它是204,它给了我疯狂的1223状态。我真的很想坚持204,因为这是最合适的回应,所以如果任何人都可以帮助弄清楚如何得到它,我会很感激。 – brianmearns 2012-04-06 14:31:27

回答

32

XMLHTTPRequest的实现还不能处理HTTP响应 的状态代码:204(无内容)是否正常;在'状态属性 价值1223

这是一个已知的bug和大多数基于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

+0

非常优秀的回复,谢谢! – brianmearns 2012-04-06 18:05:50

+0

显然IE在这种情况下也会丢弃所有响应头文件!显然它也发生在IE9中......但它已在IE10中修复。资料来源:http://www.enhanceie.com/ie/bugs.asp(ctrl + F为1223) – 2013-01-11 02:36:18

+0

感谢您的更新:)好,它终于得到修复,我想知道他们为什么没有backported修复IE9 atleast – 2013-01-11 20:14:42