Javascript 如何强制 XMLHttpRequest 仅使用 ISO-8859-1 字符集?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11906904/
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
How to force XMLHttpRequest to use ISO-8859-1 charset only?
提问by Thevs
I have ISO-8859-1 database, so I like to exchange requests entirely in this codepage. So, how to set content-type for AJAX requests in the right way?
我有 ISO-8859-1 数据库,所以我喜欢完全在这个代码页中交换请求。那么,如何以正确的方式为 AJAX 请求设置内容类型呢?
采纳答案by Hieu Nguyen
Even though it's bad to do (bunch of comments above), this would work:
即使这样做不好(上面的一堆评论),这也行得通:
var xhr = new XMLHttpRequest();
xhr.open("GET", path, false);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=ISO-8859-1')
If you are using jQuery: https://stackoverflow.com/a/553572/2527433
如果您使用 jQuery:https: //stackoverflow.com/a/553572/2527433
回答by Jay Dansand
According to the W3C spec for XMLHttpRequest.send(), the charset will end up being UTF-8 in almost all cases, depending on the value of data. Even any charset encoding you specify will likely be overwritten with UTF-8:
根据XMLHttpRequest.send()的 W3C 规范,字符集在几乎所有情况下最终都是 UTF-8,具体取决于data的值。即使您指定的任何字符集编码也可能会被 UTF-8 覆盖:
If a Content-Type header is in author request headers and its value is a valid MIME type that has a charset parameter whose value is not a case-insensitive match for encoding, and encodingis not null, set all the charset parameters of that Content-Type header to encoding.
如果 Content-Type 标头在作者请求标头中,并且其值是有效的 MIME 类型,该类型具有字符集参数,其值不是不区分大小写的encoding匹配,并且encoding不为 null,则设置该 Content 的所有字符集参数- 将标头输入到encoding。
There is some wiggle-room for the User Agent to determine the encoding: set the AJAX-containing page's encoding to ISO-8859-1. The UA will then assume ISO for all form submission (unless the form otherwise specifies a different encoding) and likely AJAX submission, depending on interpretation of the W3C algorithm.
用户代理有一些回旋余地来确定编码:将包含 AJAX 的页面的编码设置为 ISO-8859-1。然后,UA 将假定所有表单提交(除非表单另外指定不同的编码)和可能的 AJAX 提交都使用 ISO,这取决于对 W3C 算法的解释。
Ultimately, the only reliable solution is to set the page the visitor sees (with the AJAX on it) to ISO-8859-1, and then make sure to check it and convert to ISO on the back-end (you need to be sanitizing all user input before sending it to the database anyway, so just add this conversion to the process). There are plenty of library functions to do this in PHP or your given language. There's no way to guarantee conformance with the specs otherwise, so absolutely check/ensure the encoding on the back-end.
最终,唯一可靠的解决方案是将访问者看到的页面(带有 AJAX)设置为 ISO-8859-1,然后确保检查它并在后端转换为 ISO(您需要清理无论如何,在将所有用户输入发送到数据库之前,只需将此转换添加到流程中即可)。有很多库函数可以在 PHP 或您的给定语言中执行此操作。否则无法保证符合规范,因此绝对检查/确保后端的编码。
回答by Esailija
I think I need to explain encoding and the charset parameter. These concern how the raw bytes sent over the network should be decoded.
我想我需要解释编码和字符集参数。这些涉及如何解码通过网络发送的原始字节。
For example, consider the content type application/x-www-form-urlencoded
and the following data:
例如,考虑内容类型application/x-www-form-urlencoded
和以下数据:
0x61253344254345254232
Because there was no charset (in fact, charset is illegal parameter for this content type...) ISO-8859-1 must be assumed. So decoding the above in ISO-8859-1 results:
因为没有字符集(实际上,字符集是此内容类型的非法参数......)必须假设 ISO-8859-1。因此在 ISO-8859-1 结果中解码上述内容:
"a%3D%CE%B2"
Now there is another format to decode (form urlencoded) which has its own rules. The current specs say that the percent encoding here must be UTF-8, so after doing string-> stringtransformation you get from the above:
现在有另一种解码格式(形式 urlencoded),它有自己的规则。当前规范说这里的百分比编码必须是 UTF-8,因此在执行string-> string转换后,您可以从上面得到:
"a=?"
So as you can see, the format never uses characters other than ASCII so the charset doesn't really matter and is not supported anyway.
因此,如您所见,该格式从不使用 ASCII 以外的字符,因此字符集并不重要,无论如何也不支持。
Your actual problem is unrelated to what encoding the percent encoding uses. Even if you defined a custom function that percent-encodes in ISO-8859-1, the server would still have to decode it on arrival and encode it for the database. You have nothing to gain from this.
您的实际问题与百分比编码使用的编码无关。即使您定义了一个在 ISO-8859-1 中进行百分比编码的自定义函数,服务器仍然必须在到达时对其进行解码并为数据库对其进行编码。你没有任何好处。