encodeURIComponent 对 javascript 字符串使用 ISO-8859-1 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20409615/
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
encodeURIComponent using ISO-8859-1 encoding for a javascript string
提问by user1719160
I have been trying to get this work but havent got any luck so far.
I am not very clear of whats going on but I will try to explain as much as I can.
My server side jsp pages are all using ISO-8859-1 encoding which I do not want to change.
All the request/responses are in xml form.
The POST request currently is using javascript escapeURIComponent
function and everything worked well till one has special characters, for example string:hello°world???test. When this string is POSTed(with escapeURIComponent
to the data part) from IE, and when the page is reloaded which should get the same string, the string is rendered as:hello?°world????a?¢test
我一直在努力完成这项工作,但到目前为止还没有任何运气。我不是很清楚发生了什么,但我会尽量解释。我的服务器端 jsp 页面都使用 ISO-8859-1 编码,我不想更改。所有的请求/响应都是 xml 格式的。POST 请求当前使用的是 javascriptescapeURIComponent
函数,一切正常,直到出现特殊字符,例如 string:hello°world???test。当这个字符串escapeURIComponent
从 IE发布(与数据部分一起),并且当页面重新加载时应该得到相同的字符串,该字符串呈现为:hello?°world????a?¢test
I am assuming that this is happening as encodeURIComponent
function encodes the string into UTF-8, and not to ISO-8859-1, and when the page renders, the UTF-8 is interpreted as ISO-8859-1 character, and hence showing the string garbled.
我假设这是因为encodeURIComponent
函数将字符串编码为 UTF-8,而不是 ISO-8859-1,并且当页面呈现时,UTF-8 被解释为 ISO-8859-1 字符,因此显示字符串乱码。
Is there any way to solve this without converting the webpages to UTF-8 charset??
有什么方法可以解决这个问题而不将网页转换为 UTF-8 字符集?
The POST request has Content-Type set to "application/x-www-form-urlencoded"
POST 请求的 Content-Type 设置为“application/x-www-form-urlencoded”
Thanks in advance.
提前致谢。
回答by Daniel Martin
First off, I would strongly encourage you just as a general matter of principle to abandon your allegiance to ISO-8859-1 and switch to UTF-8; however, that won't solve your immediate problem, so let's leave that battle for another day.
首先,作为一般原则,我强烈建议您放弃对 ISO-8859-1 的忠诚并转向 UTF-8;然而,这并不能解决您眼前的问题,所以让我们改天再打这场仗。
encodeURIComponent
always uses UTF-8. This cannot be changed; though you could manually hack the percent encoding encodeURIComponent
produces, I don't think that would be a productive use of anyone's time.
encodeURIComponent
始终使用 UTF-8。这是无法改变的;尽管您可以手动修改百分比编码encodeURIComponent
产生的结果,但我认为这不会有效地利用任何人的时间。
From your description, I would actually place the problem further back: your server thinks that the string has those ? characters in it and so is sending back to your browser the necessary code to display those characters. Simply changing the encoding that your server is outputting would just result in your server sending the UTF-8 codes for ?, and not actually help.
根据您的描述,我实际上会将问题放回更远的地方:您的服务器认为该字符串具有那些 ? 其中的字符,因此将必要的代码发送回您的浏览器以显示这些字符。简单地更改您的服务器输出的编码只会导致您的服务器发送 UTF-8 代码?,实际上并没有帮助。
So the issue is: how do we tell the serverthat the incoming data is percent-encoded UTF-8 and not, as the server apparently believes, percent-encoded 8859-1?
所以问题是:我们如何告诉服务器传入的数据是百分比编码的 UTF-8,而不是服务器显然认为的百分比编码的 8859-1?
You don't specify in your post whether the string you're sending is being sent as part of the URL (that is, you're POSTing to some URL like http://myserver/mypage.jsp?theString=hello%C2%B0world%C2%A9%C2%AE%E2%84%A2test
) or as part of the POST body. Normally with a POST you send data as part of the POST body. If that's the case, try adding
您没有在您的帖子中指定您发送的字符串是作为 URL 的一部分发送(也就是说,您正在发送到某个 URL,如http://myserver/mypage.jsp?theString=hello%C2%B0world%C2%A9%C2%AE%E2%84%A2test
)还是作为 POST 正文的一部分。通常使用 POST 将数据作为 POST 正文的一部分发送。如果是这种情况,请尝试添加
<% request.setCharacterEncoding("UTF-8"); %>
to the top of your jsp - that tells the server to interpret incoming requests as being in UTF-8, even if outgoing stuff is still 8859-1. If you have any <form>
elements pointing at this page, you should add an accept-charsetattribute to the form that says "UTF-8".
到您的 jsp 的顶部 - 告诉服务器将传入的请求解释为 UTF-8,即使传出的内容仍然是 8859-1。如果您有任何<form>
指向此页面的元素,您应该向显示“UTF-8”的表单添加一个accept-charset属性。
If by chance what you're passing is in the URL itself, then you need set the URIEncoding
on whatever servlet container you're using; if it's Tomcat, see this question's answer.
如果您偶然传递的是 URL 本身,那么您需要URIEncoding
在您使用的任何 servlet 容器上设置;如果是 Tomcat,请参阅此问题的答案。