我知道这听起来很糟糕,但这是必要的。
我在一个带有 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
。
编辑使用encodeURIComponent
服务器理解art%C3%ADculo
函数。附言我只是使用 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>
关于Javascript 将字符串从 utf-8 转换为 iso-8859-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36495416/