Working As Intended

Firefox 3.5.2 and XMLHttpRequest.setRequestHeader()

August 10th 2009

It appears that Firefox 3.5.2 broke XMLHttpRequest.setRequestHeader(). According to the W3C docs, in giant bold green type:

The setRequestHeader() method appends a value if the HTTP header given as argument is already part of the list of request headers.

Rather than appending, it’s overwriting the existing value entirely.

The particular case where I ran into this was when using jQuery to send a remote form request. My setup code includes the following line.

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

When firing off an Ajax remote form request with Safari or older (pre-3.5) versions of Firefox, the header would contain the following.

Accept: application/javascript, */*, text/javascript

Or, if the browser had already decided that it was going to accept text/javascript, at worst I’d get a duplicate.

Accept: text/javascript, application/javascript, */*, text/javascript

With Firefox 3.5.2 I see this instead.

Accept: text/javascript

And the HTTP error returned from the server.

406 Not Acceptable

The workaround is simple.

xhr.setRequestHeader("Accept", "text/javascript,application/javascript,text/html")

I have to set that header parameter with all of the types that should be accepted as a response to a remote form request.

The downside to this is that non-broken browsers almost defintely include duplicate values, but at least it still works.

Ex.: Safari with the workaround in place.

Accept: text/javascript, application/javascript, */*, text/javascript,application/javascript,text/html
blog comments powered by Disqus

Feed Feed