簡體   English   中英

"ajax、setRequestHeader()、Content-Type、application\/x-www-form-urlencoded 和 charset"

[英]ajax, setRequestHeader(), Content-Type, application/x-www-form-urlencoded and charset

當內容類型不是 text/html、text/plain 或 text/xml,而是 application/x-www-form-urlencoded 內容類型時,我無法理解如何設置字符集。

鑒於這個(簡化的)javascript代碼:

var xhr = new XMLHttpRequest();

如果我沒有明確設置編碼,

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

firebug 告訴我內容類型是“application/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');

firebug仍然告訴我“application/x-www-form-urlencoded; charset= UTF-8 。”

如果我嘗試類似

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

然后它尊重字符集。

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

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

如果 Content-Type 是 x-www-form-urlencoded,為什么它不遵守我指定的字符集?

注意:我以 ISO-8859-1 為例。 我的目標是了解發生了什么。

application/x-www-form-urlencoded mime類型不支持參數(例如charset )。 如果您查看HTML5規范的這一部分 ,您將看到如何確定字符集(它很復雜)。 特別是,在該部分的底部有一個注釋,提到如何將charset指定為mime類型的參數。

你可以試試這段代碼:

.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM