Ajax, setRequestHeader (), Content-Type, application / x-www-form-urlencoded and charset

I am having trouble understanding how to set the encoding when the content type is not text / html, text / plain or text / xml, but instead the application content type is / x -www-form-urlencoded.

Given this (simplified) javascript code:

var xhr = new XMLHttpRequest(); 

If I did not explicitly set the encoding,

 xhr.open('POST', 'serv.php', true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

firebug tells me that the content type is "application / x-www-form-urlencoded; charset = UTF-8 ."

If I set the encoding to ISO-8859-1, for example,

 xhr.open('POST', 'serv.php', true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1'); 

firebug still tells me "application / x-www-form-urlencoded; charset = UTF-8 ."

If I try something like

 xhr.setRequestHeader('Content-Type', 'text/plain; charset=ISO-8859-1'); 

then he observes the encoding.

In all cases, the send () method looks like this:

 xhr.send('id=9&name=Yoda'); 

Why doesn't he follow the encoding that I specify if the Content-Type is x-www-form-urlencoded?

NOTE. I am using ISO-8859-1 as an example. My goal is to understand what is happening.

+8
javascript ajax character-encoding
source share
1 answer

The mime type application/x-www-form-urlencoded does not support parameters (e.g. charset ). If you look at this section of the HTML5 specification, you will see how the encoding is defined (it is complex). In particular, at the bottom of the section there is a note on how the encoding cannot be specified as a parameter for the mime type.

+11
source share

All Articles