如何将 Javascript 字符串转码为 ISO-8859-1?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2283829/
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 do I transcode a Javascript string to ISO-8859-1?
提问by Marcos Marin
I'm writing a Chrome extension that works with a website that uses ISO-8859-1. Just to give some context, what my extension does is making posting in the site's forums quicker by adding a more convenient post form. The value of the textarea where the message is written is then sent through an Ajax call (using jQuery).
我正在编写一个适用于使用 ISO-8859-1 的网站的 Chrome 扩展程序。只是为了提供一些上下文,我的扩展程序的作用是通过添加更方便的帖子表单来更快地在网站论坛中发帖。然后通过 Ajax 调用(使用 jQuery)发送写入消息的 textarea 的值。
If the message contains characters like áthese characters appear as ?? in the posted message. Forcing the browser to display UTF-8 instead of ISO-8859-1 makes the áappear correctly.
如果消息包含类似á这些字符的字符,则显示为 ?? 在发布的消息中。强制浏览器显示 UTF-8 而不是 ISO-8859-1 使á显示正确。
It is my understanding that Javascript uses UTF-8 for its strings, so it is my theory that if I transcode the string to ISO-8859-1 before sending it, it should solve my problem. However there seems to be no direct way to do this transcoding in Javascript, and I can't touch the server side code. Any advice?
我的理解是 Javascript 对其字符串使用 UTF-8,所以我的理论是,如果我在发送之前将字符串转码为 ISO-8859-1,它应该可以解决我的问题。但是似乎没有直接的方法可以在 Javascript 中进行这种转码,而且我无法触及服务器端代码。有什么建议吗?
I've tried setting the created form to use iso-8859-1 like this:
我尝试将创建的表单设置为使用 iso-8859-1,如下所示:
var form = document.createElement("form");
form.enctype = "application/x-www-form-urlencoded; charset=ISO-8859-1";
And also:
并且:
var form = document.createElement("form");
form.encoding = "ISO-8859-1";
But that doesn't seem to work.
但这似乎不起作用。
EDIT:
编辑:
The problem actually lied in how jQuery was urlencoding the message (or something along the way), I fixed this by telling jQuery not to process the data and doing it myself as is shown in the following snippet:
问题实际上在于 jQuery 如何对消息进行 urlencoding(或沿途的其他东西),我通过告诉 jQuery 不要处理数据并自己处理数据来解决这个问题,如下面的代码片段所示:
function cfaqs_post_message(msg) {
var url = cfaqs_build_post_url();
msg = escape(msg).replace(/\+/g, "%2B");
$.ajax({
type: "POST",
url: url,
processData: false,
data: "message=" + msg + "&post=Preview Message",
success: function(html) {
// ...
},
dataType: "html",
contentType: "application/x-www-form-urlencoded"
});
}
回答by Arthur Ronald
It is my understanding that Javascript uses UTF-8 for its strings
我的理解是 Javascript 使用 UTF-8 作为其字符串
No, no.
不,不。
Each page has its charset enconding defined in meta tag, just belowhead element
每个页面都有在元标记中定义的字符集编码,就在head 元素下方
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
or
或者
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>
Besides that, each page should be edited with the target charset encoding. Otherwise, it will not work as expected.
除此之外,每个页面都应该使用目标字符集编码进行编辑。否则,它将无法按预期工作。
And it is a good idea to define its target charset encoding on server side.
在服务器端定义其目标字符集编码是一个好主意。
Java
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
PHP
header("Content-Type: text/html; charset=UTF-8");
C#
I do not know how to...
And it could be a good idea to set up each script file whether it uses sensitive characters (á, é, í, ó, ú and so on...).
并且最好设置每个脚本文件是否使用敏感字符(á、é、í、ó、ú 等...)。
<script type="text/javascript" charset="UTF-8" src="/PATH/TO/FILE.js"></script>
...
...
So it is my theory that if I transcode the string to ISO-8859-1 before sending it, it should solve my problem
所以我的理论是,如果我在发送之前将字符串转码为 ISO-8859-1,它应该可以解决我的问题
No, no.
不,不。
The target server could handle strings in other than ISO-8859-1. For instance, Tomcat handles in ISO-8859-1, no matter how you set up your page. So, on server side, you could have to set up your request according how your set up your page.
目标服务器可以处理 ISO-8859-1 以外的字符串。例如,Tomcat 处理 ISO-8859-1,无论您如何设置页面。因此,在服务器端,您可能必须根据您设置页面的方式来设置您的请求。
Java
request.setCharacterEncoding("UTF-8")
PHP
// I do not know how to...
If you really want to translate the target charset encoding, TRY as follows
如果你真的要翻译目标字符集编码,请尝试如下
InternetExplorer
formElement.encoding = "application/x-www-form-urlencoded; charset=ISO-8859-1";
ELSE
formElement.enctype = "application/x-www-form-urlencoded; charset=ISO-8859-1";
Oryou should provide a function that gets the numeric representation, in Unicode Character Set, used by each character. It will work regardless of the target charset encoding. For instance, á as Unicode Character Set is \u00E1;
或者您应该提供一个函数来获取每个字符使用的 Unicode 字符集中的数字表示。无论目标字符集编码如何,它都可以工作。例如,作为 Unicode 字符集的 á 是 \u00E1;
alert("á without its Unicode Character Set numerical representation");
function convertToUnicodeCharacterSet(value) {
if(value == "á")
return "\u00E1";
}
alert("á Numerical representation in Unicode Character Set is: " + convertToUnicodeCharacterSet("á"));
Hereyou can see in action:
在这里您可以看到实际操作:
You can use this linkas guideline (See JavaScript escapes)
您可以使用此链接作为指导(请参阅 JavaScript 转义)
Added to original answer how I implement jQuery funcionality
添加到原始答案中我如何实现 jQuery 功能
var dataArray = $(formElement).serializeArray();
var queryString = "";
for(var i = 0; i < dataArray.length; i++) {
queryString += "&" + dataArray[i]["name"] + "+" + encodeURIComponent(dataArray[i]["value"]);
}
$.ajax({
url:"url.htm",
data:dataString,
contentType:"application/x-www-form-urlencoded; charset=UTF-8",
success:function(response) {
// proccess response
});
});
It works fine without any headache.
它工作正常,没有任何头痛。
Regards,
问候,
回答by Sergio
I had a very similar problem. I needed to pass a URL parameter using JQuery to make an ajax call, and most of the times parameters values included accents.
我有一个非常相似的问题。我需要使用 JQuery 传递一个 URL 参数来进行 ajax 调用,并且大多数时候参数值都包含重音符号。
Both pages had to be set to charset=ISO-8859-1 and javascript's functions: encodeURI, encodeURIComponent etc. only uses UTF-8.
两个页面都必须设置为 charset=ISO-8859-1 并且 javascript 的函数:encodeURI、encodeURIComponent 等仅使用 UTF-8。
What I did was to create a link in the original page, including all parameters without any encoding, let's say:
我所做的是在原始页面中创建一个链接,包括没有任何编码的所有参数,让我们说:
var myLink = document.getElementById("myHiddenLink");
myLink.setAttribute("href", "México, Perú, María and any other words with accents and spaces");
and then assign the href value to a variable, like this:
然后将 href 值分配给一个变量,如下所示:
var theLink = myLink.getAttribute("href");
So finally "theLink" variable value was ISO-8859-1 encoded, and everything worked just fine.
所以最后“theLink”变量值是 ISO-8859-1 编码的,一切正常。

