Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSIE Returns status code of 1223 for Ajax Request

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.

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.

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);
like image 756
brianmearns Avatar asked Apr 06 '12 14:04

brianmearns


1 Answers

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.

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

Reference:

dojo - http://trac.dojotoolkit.org/ticket/2418

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

like image 106
Saket Patel Avatar answered Oct 11 '22 13:10

Saket Patel