javascript 使用 Firefox 发送的 POST 请求中的字符编码问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12463800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:16:09  来源:igfitidea点击:

Issue with character encoding in POST requests sent with Firefox

javascriptajaxfirefoxcharacter-encoding

提问by Andrei Oniga

Recently I've come across some very strange behavior related to character encoding for AJAX calls made using the POST method. To make a long story short, I have an HTML form with text fields that can accept diacritics (e.g. "?"). When the form is submitted, the form data is wrapped in an XML block and sent to a server, which stores that information in a MySQL database. Subsequently, that information is retrieved from the database and displayed to regular users, as is.

最近,我遇到了一些与使用 POST 方法进行的 AJAX 调用的字符编码相关的非常奇怪的行为。长话短说,我有一个 HTML 表单,其中的文本字段可以接受变音符号(例如“ ”)。当表单被提交时,表单数据被包装在一个 XML 块中并发送到服务器,该服务器将该信息存储在 MySQL 数据库中。随后,从数据库中检索该信息并按原样显示给普通用户。

If the request is sent from Chrome or IE, everything is fine. This means that the data, including the diacritics, is sent, stored, then retrieved and displayed correctly. However, when I use Firefox for this, the XML appears to submit the form data right, but when I reload the web page, the previously sent diacritics don't appear. In other words, they seem to get lost somewhere along the way. For example, if the XML contains the word "t?st?", when I load the page I see "tst".

如果请求是从 Chrome 或 IE 发送的,则一切正常。这意味着数据(包括变音符号)将被正确发送、存储、检索和显示。但是,当我为此使用 Firefox 时,XML 似乎正确提交了表单数据,但是当我重新加载网页时,之前发送的变音符号不会出现。换句话说,他们似乎在途中迷路了。例如,如果 XML 包含单词“ t?st?”,当我加载页面时,我会看到“ tst”。

Why is this happening? Is Firefox encoding the post messages differently from IE and Chrome?

为什么会这样?Firefox 对帖子消息的编码是否与 IE 和 Chrome 不同?

In case it helps, I've attached the request and response headers from Chrome and Firefox, for exactly the same form content - only one example:

如果有帮助,我附上了来自 Chrome 和 Firefox 的请求和响应标头,用于完全相同的表单内容 - 只有一个示例:

By the way, I'm not encoding the data before sending it to the server, just simply retrieving the value of the form fields, as is.

顺便说一下,在将数据发送到服务器之前,我没有对数据进行编码,只是简单地按原样检索表单字段的值。

CHROME:

铬合金:

The XML data block:

XML 数据块:

<request>
<session>{hidden by me}</session>
<builder>Hem i Stan t?¤st?¤</builder>
</request>

The request headers:

请求标头:

Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:562
Content-Type:application/x-www-form-urlencoded
Cookie:PHPSESSID=rlne2d787j0np52ec5rtn04dm1
Host:83.150.87.220
Origin:http://hidden.by.me
Referer:http://http://hidden.by.me/?c=2094211
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
X-Requested-With:XMLHttpRequest

The response headers:

响应头:

Connection:Keep-Alive
Content-Encoding:gzip
Content-Type:application/xml
Date:Mon, 17 Sep 2012 16:21:58 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.11 (Win32) PHP/5.2.9-1
Transfer-Encoding:chunked
Vary:Accept-Encoding

FIREFOX:

火狐:

The XML data block:

XML 数据块:

<request>
<session>{hidden by me}</session>
<builder>Hem i Stan t?st?</builder>
</request>

The request headers:

请求标头:

Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Content-Length  562
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie  PHPSESSID=kvfg4fp2trorllim19dmn241c7
Host    hidden.by.me
Referer http://hidden.by.me/?c=2094211
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1
X-Requested-With    XMLHttpRequest

The response headers:

响应头:

Connection  Keep-Alive
Content-Encoding    gzip
Content-Type    application/xml
Date    Mon, 17 Sep 2012 16:21:23 GMT
Keep-Alive  timeout=5, max=100
Server  Apache/2.2.11 (Win32) PHP/5.2.9-1
Transfer-Encoding   chunked
Vary    Accept-Encoding

采纳答案by Andrei Oniga

As @Pointy mentioned some time ago, the problem was related to the Content-Typeof the POSTrequest, because Firefox appears to encode POSTmessages differently than other browsers. In my head, Data-Typeand Content-Typewere the same and so, I didn't realize it's necessary to specify UTF-8as the character encoding standard in both cases. But once I had changed both the Content-Typeand the Data-Typeas well to a clear "text/xml; charset=UTF-8", the problem was resolved.

作为@Pointy提到前一段时间,问题涉及到Content-Type的的POST要求,因为火狐似乎编码POST信息不同于其他浏览器。在我的脑海中,Data-Type并且Content-Type是相同的,因此,我没有意识到有必要UTF-8在两种情况下都指定为字符编码标准。但是,一旦我将 theContent-Type和 the都更改Data-Type为明确的“text/xml; charset=UTF-8”,问题就解决了。

回答by Roger F. Gay

I am soooo happy. Thank you guys for posting and figuring this out earlier. It took me a couple hours to get close enough to the problem to find this through googling, but because of your comments, I got this solved in less than a day; and in time for the big presentation tomorrow! :)

我太高兴了。感谢你们提前发布并解决了这个问题。我花了几个小时才接近问题,通过谷歌搜索找到了这个问题,但由于你的评论,我在不到一天的时间内解决了这个问题;赶上明天的大型演讲!:)

It was so bizarre, seeing that all browsers were sending the very same data string in an AJAX request but getting different results, depending on the browser (Firefox being different.)

这太奇怪了,看到所有浏览器都在 AJAX 请求中发送完全相同的数据字符串,但得到不同的结果,这取决于浏览器(Firefox 不同。)

I tried this, but it didn't work:

我试过这个,但没有用:

req.setRequestHeader ("encoding", "utf-8");

req.setRequestHeader ("encoding", "utf-8");

Then I just did what you said Firefox does and one coding solution works in all browsers.

然后我只是做了你所说的 Firefox 所做的,一种编码解决方案适用于所有浏览器。

req.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");

req.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");

I've tested on Chrome, MSIE, Firefox, Safari, Opera and Opera Next. Works every time!

我已经在 Chrome、MSIE、Firefox、Safari、Opera 和 Opera Next 上进行了测试。每次都有效!