2013-10-30 23 views
8

我無法理解如何在 內容類型不是text/html,text/plain或text/xml,而是應用程序/ x-www-form-urlencoded內容類型。ajax,setRequestHeader(),Content-Type,application/x-www-form-urlencoded和charset

鑑於此(簡化)的javascript代碼:

var xhr = new XMLHttpRequest(); 

如果我明確地設置編碼,

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

螢火告訴我,內容 類型是「應用程序/ x -www-form-urlencoded; charset = UTF-8。「

如果我設置的字符集爲ISO-8859-1例如,

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

螢火仍然告訴我:「應用程序/ x-WWW窗體-urlencoded;字符集= UTF-8。 「

如果我嘗試像

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

那麼尊重的字符集。

在所有的情況下,send()方法是這樣的:

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

爲什麼它不兌現如果Content-Type是X WWW的形式,進行了urlencoded我指定的字符集?

注意:我僅以ISO-8859-1爲例。我的目標是瞭解正在發生的事情。

回答

11

application/x-www-form-urlencoded mime類型不支持參數(如charset)。如果你看看HTML5規範的this section,你會看到charset是如何確定的(這很複雜)。特別是,在該部分的底部有一個提示,說明不能將charset指定爲mime類型的參數。