最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ajax, setRequestHeader(), Content-Type, applicationx-www-form-urlencoded and charset - Stack Overflow

programmeradmin4浏览0评论

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

Given this (simplified) javascript code:

var xhr = new XMLHttpRequest();

If I do 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 charset to ISO-8859-1 for instance,

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 it respects the charset.

In all the cases the send() method goes like this:

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

Why doesn't it honor the charset I specify if the Content-Type is x-www-form-urlencoded?

NOTE: I am using ISO-8859-1 just as an example. My goal is to understand what is going on.

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

Given this (simplified) javascript code:

var xhr = new XMLHttpRequest();

If I do 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 charset to ISO-8859-1 for instance,

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 it respects the charset.

In all the cases the send() method goes like this:

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

Why doesn't it honor the charset I specify if the Content-Type is x-www-form-urlencoded?

NOTE: I am using ISO-8859-1 just as an example. My goal is to understand what is going on.

Share Improve this question edited Oct 31, 2013 at 13:34 Fernando Basso asked Oct 30, 2013 at 21:53 Fernando BassoFernando Basso 7081 gold badge11 silver badges27 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

The application/x-www-form-urlencoded mime type does not support parameters (such as charset). If you look at this section of the HTML5 spec, you will see how charset is determined (it's complicated). In particular, there is a note at the bottom of the section mentioning how charset cannot be specified as a parameter to the mime type.

You can try this code:

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论