Why non-ascii chars are displayed as weird symbols?

白昼怎懂夜的黑 提交于 2019-12-01 21:01:03
Aaron Digulla

The means that you used a character which can't be represented properly.

Somewhere between the server and the client, you need to encode the string data properly. I don't know how you transfer the data from the server to the client (generate JavaScript, AJAX, GET requests), it's hard to say how to fix this.

But what you need to do: For every step, you must make sure that you know what the current encoding of the data is and what the recipient expects.

For example if you generate inline JavaScript in a HTML page, then the string value must be encoded with the same encoding as the page (iso-8859-1). If you use AJAX, then usually you have to use UTF-8.

user2517028

Thanks to Aaron Digulla's answer, I followed the string from server to the page and found that it is gets misrepresented after the AJAX LOAD, so I found this answer which resolved my problem. Although I had to use the charset="iso-8859-1" for it to work rather than charset="UTF-8".

So the final answer is:

-Encoding in the HTML page:

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

-Encoding the Ajax data:

 $.ajaxSetup({
          'beforeSend' : function(xhr) {
           xhr.overrideMimeType('text/html; charset=iso-8859-1');
        },
    });

And now chars are displayed correctly. Thanx for your help guys..

GCallie

Javascript default encoding for strings is UTF-16 (16 bits) while iso-8859-1 is 8 bits.

Default Javascript Character Encoding?

I think you can use encodeURI() to convert your special characters to ASCII characters and afterwards you can decode it with decodeURI()

http://www.w3schools.com/jsref/jsref_encodeuri.asp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!