Firefox 3.x appends "charset=UTF-8" to Content-Type header

wmethlie
wmethlie used Ask the Experts™
on
We are developing an ajax app that sends and receives JSON data via xhr, and have run into a problem with FF3+ (and potentially something similar in Webkit browsers).

When sending POST requests to the server, we set the content type to "text/json", and IE sends this along with no problem.  FF on the other hand, appends "; charset=UTF-8" to the end of the header, regardless of what was set in the JavaScript.  So, for example, if we set it like this:

   myxhr.setRequestHeader( "Content-type", "text/json" );

when looking at the request headers in Firebug, we see:

  Content-type: text/json; charset=UTF-8

Normally this is not a problem, but it seems that the proprietary MS chat server we are using (CWA), is doing a straight string comparison.  We think it looks somethign like this (pseudo-code):

  if( request.responseType == "text/json" )
     processRequest()
  else
     throwException()

Because we cannot change the server code (without a month long MS support marathon - usually ending in a big "no"), we need a way to override this behavior in FF. Of course we cannot expect our users to go into their "about:config" and change settings, so the solution would have to be javascript-based.

We have also toyed with a server-side solution.  While we can't change the CWA code, we can manage the instance of IIS sitting between our request and the CWA server that ultimately processes it, so we considered having IIS inspect and strip out the "; charset=UTF-8" part of the content-type header for all requests to a particular page.  How difficult would this be, and how much of a performance hit would we take?  (It's a very high volume system).

Thanks in advance,

William
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pawel WitkowskiSenior Javascript Developer
Top Expert 2008

Commented:
There is some information about your problem, but most of them unfortunatelly says that it cant be done from javascript side:

http://forums.mozillazine.org/viewtopic.php?f=25&t=966035


There is some kind of solution for Apache, but this probably wont help you:

http://forums.mozillazine.org/viewtopic.php?f=25&t=678205




You can read about w3 specification for XHR:

http://www.w3.org/TR/XMLHttpRequest/

there is some useful info for send() object:

"If stored method is GET act as if the data argument is null.

If the data argument has not been omitted and is not null use it for the entity body as defined by section 7.2 of RFC 2616 observing the following rules: [RFC2616]

data is a DOMString
Encode data using UTF-8 for transmission.

If a Content-Type header is set using setRequestHeader() set the charset parameter of that header to UTF-8.

data is a Document
Serialize data into a namespace well-formed XML document and encoded using the encoding given by data.inputEncoding, when not null, or UTF-8 otherwise. Or, if this fails because the Document cannot be serialized act as if data is null.

If no Content-Type header has been set using setRequestHeader() append a Content-Type header to the list of request headers with a value of application/xml;charset=charset where charset is the encoding used to encode the document.

Subsequent changes to the Document have no effect on what is submitted.

data is not a DOMString or Document
Use the stringification mechanisms of the host language on data and treat the result as if data is a DOMString. Or, if this fails, act as if the data argument is null.

If the data argument has been omitted, or is null, no entity body is used in the request."







Some other problems for ppl...:

http://techblogbydave.blogspot.com/2008/08/firefox-3-content-type-header-conflict.html



If you cant do anything on server page then... well I would advice to manually get data on server side and to process headers by yourself (instead of doing this automatically if possible). Cutting out a headers in fly is also some kind of solution but really cant tell you how much load this will generate, especially that there would be alot of those requests.

I'm not envy you with this really difficult problem, but probably there is no known javascript solution for your problem :(
Commented:
We contacted the authors of our server software and they have opened a bug ticket.  The "problem" was really a consequence of FF correctly implementing the "Content-type" spec, which requires server-side software to implement the same spec.  Our server-side code did not implement the spec, and we are asking them to do that now.

Thanks to all who offered advice.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial