Javascript 将字符串从 utf-8 转换为 iso-8859-1
Javascript convert string from utf-8 to iso-8859-1
我知道这听起来很糟糕,但这是必要的。
我在一个带有 utf-8 字符集的站点上有一个 HTML 表单,它被发送到一个使用 iso-8859-1 字符集的服务器。问题是服务器不能正确理解我们在西班牙使用的字符,如 à, á, è, é, ì, í, ò, ó, ù, ú, ñ, ç
等。因此,如果我搜索类似 artículo
的内容,它不会回答 artÃculo
.
我使用 ajaxform (http://malsup.com/jquery/form/) 发送表单,代码如下所示:
$(".form-wrap, #pagina").on("submit", "form", function(event){
event.preventDefault();
$(this).ajaxSubmit({
success: function(data){
$("#temp").html(data);
//Handle data in #temp div
$("#temp").html('');
}
});
return false;
});
我的问题是:我无法访问搜索服务器,而且我无法将整个网站更改为 iso-8859-1 编码,因为这会破坏其他内容。
我已经尝试过这些脚本但没有成功:
- http://phpjs.org/functions/utf8_decode/
- http://phpjs.org/functions/utf8_encode/
- http://ecmanaut.blogspot.com.es/2006/07/encoding-decoding-utf8-in-javascript.html
我可能做错了吗?
编辑: escape
函数对我没有用,因为它将这些空格字符转换为对服务器无用的 %
前缀代码,然后搜索 art%EDculo
.
编辑 使用服务器理解 art%C3%ADculo
的 encodeURIComponent
功能。 P.S。我只是使用 artículo
这个词进行测试,但它应该涵盖所有特殊字符的解决方案。
I have a HTML form on a site with utf-8 charset which is sent to a server which works with the iso-8859-1 charset.
您可以尝试设置表格 accept-charset:
<form accept-charset="iso-8859-1">
....
</form>
最后,运行该服务器的人允许我们访问该服务器,我们制作了一个 utf-8 兼容的 php 脚本。所以我不在乎了。
我知道这听起来很糟糕,但这是必要的。
我在一个带有 utf-8 字符集的站点上有一个 HTML 表单,它被发送到一个使用 iso-8859-1 字符集的服务器。问题是服务器不能正确理解我们在西班牙使用的字符,如 à, á, è, é, ì, í, ò, ó, ù, ú, ñ, ç
等。因此,如果我搜索类似 artículo
的内容,它不会回答 artÃculo
.
我使用 ajaxform (http://malsup.com/jquery/form/) 发送表单,代码如下所示:
$(".form-wrap, #pagina").on("submit", "form", function(event){
event.preventDefault();
$(this).ajaxSubmit({
success: function(data){
$("#temp").html(data);
//Handle data in #temp div
$("#temp").html('');
}
});
return false;
});
我的问题是:我无法访问搜索服务器,而且我无法将整个网站更改为 iso-8859-1 编码,因为这会破坏其他内容。
我已经尝试过这些脚本但没有成功:
- http://phpjs.org/functions/utf8_decode/
- http://phpjs.org/functions/utf8_encode/
- http://ecmanaut.blogspot.com.es/2006/07/encoding-decoding-utf8-in-javascript.html
我可能做错了吗?
编辑: escape
函数对我没有用,因为它将这些空格字符转换为对服务器无用的 %
前缀代码,然后搜索 art%EDculo
.
编辑 使用服务器理解 art%C3%ADculo
的 encodeURIComponent
功能。 P.S。我只是使用 artículo
这个词进行测试,但它应该涵盖所有特殊字符的解决方案。
I have a HTML form on a site with utf-8 charset which is sent to a server which works with the iso-8859-1 charset.
您可以尝试设置表格 accept-charset:
<form accept-charset="iso-8859-1">
....
</form>
最后,运行该服务器的人允许我们访问该服务器,我们制作了一个 utf-8 兼容的 php 脚本。所以我不在乎了。