Prototype returning status code 0 in Opera
Seems like browsers don't handle Ajax calls returning a 204 no content status code very well. I've had issues with IE7 gets a 1223 status code instead of the 204. Well, it looks like Opera, at least 9.5.1, gets 0 as the status code.
I'm using Prototype version 1.6.0.3 to handle the Ajax call so I've got onSuccess, onFailure, on204, etc handlers setup along with an on1223 handler for the IE7 issue. Since I've already done that workaround, this one was pretty easy. I just added an on0 handler.
PHP:
-
new Ajax.Request(url,
-
{
-
// Unimportant stuff...
-
on204: function(r)
-
{
-
// Handle the 204
-
},
-
on0: function(r)
-
{
-
r.request.options.on204();
-
}
-
// Unimportant stuff...
-
})
There's bound to be a better way, but this works for now.
Question, Comments...
Do you have more questions. Please either leave a comment below or join us in our new forum.
Good post sir. I ran into the same problem for which there is no alternative accept to use a different, less “spec”, status. In the HTTP 1.1 spec it says of the 204 No Content status code:
“…The response MAY include new or updated meta information in the form of entity-headers, which if present SHOULD be associated with the requested variant.”
Yet, the accompanying header information for this status is not available to either IE (as of v 7.x) or Opera (as of v 9.6) via the XMLHTTPRequest object.
Fortunately the Prototype authors let the on### functions work for any number. If they had just made it work with the standard response codes none of this would work.
A problem is that there is no way to distinguish a correct 204 response from a network failure in Opera, since both cases result in a status code of 0, and getAllResponseHeaders() returns an empty string.
@Sergiu – True, although in my case that doesn’t really matter. The app I was writing when this came up should display a similar enough message between a 204 or network failure.
But realistically, how often is that going to happen? You’re already browsing to the site when you load the JS. The JS should hit the same domain asking for an Ajax response so unless your connection drops between downloading the JS and the Ajax request that shouldn’t ever happen. Or, at least it seems that way. Probably assuming too much on my part though.