我正在编写一个与使用ISO-8859-1的网站配合使用的Chrome扩展程序.只是为了给出一些背景信息,我的扩展功能是通过添加更方便的帖子表单来更快地在网站的论坛中发帖.然后通过Ajax调用(使用jQuery)发送写入消息的textarea的值.
如果邮件包含á这些字符,则在发布的邮件中显示为Ã.强制浏览器显示UTF-8而不是ISO-8859-1使得á显示正确.
我的理解是Javascript使用UTF-8作为其字符串,因此我的理论是,如果我在发送之前将字符串转码为ISO-8859-1,它应该可以解决我的问题.然而似乎没有直接的方法在Javascript中进行这种转码,我无法触及服务器端代码.有什么建议?
我已经尝试将创建的表单设置为使用iso-8859-1,如下所示:
var form = document.createElement("form");
form.enctype = "application/x-www-form-urlencoded; charset=ISO-8859-1";
Run Code Online (Sandbox Code Playgroud)
并且:
var form = document.createElement("form");
form.encoding = "ISO-8859-1";
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.
编辑:
问题实际上是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"
});
}
Run Code Online (Sandbox Code Playgroud) javascript utf-8 iso-8859-1 transcoding google-chrome-extension