通过XMLHTTPRequest发布时无法设置自定义编码 [英] Impossible to set a custom encoding when post trough XMLHTTPRequest

查看:109
本文介绍了通过XMLHTTPRequest发布时无法设置自定义编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从最新版Chrome浏览器的JS控制台:

From the JS console of the last version of chrome browser :

x = new XMLHttpRequest();
x.open('POST', '?a=2');
x.setRequestHeader('Content-Type', 
                   'application/x-www-form-urlencoded; charset="ISO-8859-1"');
x.send({b:2});

请求在网络检查器中以错误的编码发送,我明白了:

Content-Type:application/x-www-form-urlencoded; charset="UTF-8"

另一个非常奇怪的行为,当我提供 charset = ISO-8859-1 而不是 charset =ISO-8859-1,我得到:

Another pretty strange behavior, when i provide charset=ISO-8859-1 instead of charset="ISO-8859-1", I get :

Content-Type:application/x-www-form-urlencoded; charset=UTF-8

因为两者之间发生了变化。

As something had change in between.

编辑2013/12/11

嗯,重点对我来说真的很麻烦,如果你不发送(数据),编码改变就会按预期工作。这需要我之前的例子(注意最后一行的变化):

Well, the point that is really buggy for me is that if you do not send (data), the encoding change works as expected. It take my previous example (note the change on the last line) :

x = new XMLHttpRequest();
x.open('POST', '?a=2');
x.setRequestHeader('Content-Type', 
                   'application/x-www-form-urlencoded; charset="ISO-8859-1"');
x.send();

然后网络正确输出:

Content-Type:application/x-www-form-urlencoded; charset="ISO-8859-1"

奇怪......

推荐答案

W3C XMLHttpRequest规范指示字符集始终为UTF-8,指定另一个字符集不会强制浏览器更改编码。

The W3C XMLHttpRequest specification dictates that the charset is always UTF-8, specifying another charset will not force the browser to change the encoding.

请注意文档在设置Content-Type

Note that the documentation for jQuery's $.ajax method says the exact same thing when it comes to setting the Content-Type

这篇关于通过XMLHTTPRequest发布时无法设置自定义编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆