本文介绍了将ISO-8859-1转换为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过jQuery的ajax发送HTTP请求。

I am sending a HTTP request through jQuery's ajax.

但是我无法访问的服务器返回ISO-8859-1而我的页面是UTF-8。

But the server, which I have no access, returns ISO-8859-1 and I my page is UTF-8.

如何将字符转换为可读?

How can I convert the characters to be readable?

如果没有转换,就会出现类似于:it rio

For without converting appears something like: it�rio

@Edit:
我是尝试使用以下方法更改ajax请求的字符集:

@I've tried changing the charset of ajax requests using:

$.ajax({ contentType: ... });

我试图将html的meta更改为ISO-8859-1。

And I tried to change the meta of the html to ISO-8859-1.

@Solution:
我找到了解决方案:

@Solution:I've found the solution on: https://stackoverflow.com/a/14397845/3451442

推荐答案

尝试以下所示的技巧:

Try the trick shown in: How do I convert special UTF-8 chars to their iso-8859-1 equivalent using javascript?

在您的情况下,您只需使用:

in your case you could simply use:

utfstring = unescape(encodeURIComponent(ajaxreturn));

编辑:如果这也不起作用,请尝试反过来:

if this does not work either, try the other way round:

fixedstring = decodeURIComponent(escape(ajaxreturn));

如果您发布编码函数的输出,这也可能会有所帮助 - 这样就可以确定发生了什么:

It might also help if you post the output of the "encode" functions - this way its possible to identify what's going on:

encodeURIComponent(ajaxreturn)
        vs.
escape(ajaxreturn)

如果这些都不起作用我想你应该将整个页面转换为iso-8859-1:

If none of these work I guess you should convert your whole page to be iso-8859-1:

 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

这篇关于将ISO-8859-1转换为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:10