本文介绍了在JavaScript中显示带重音符号的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中显示重音字符时遇到问题;它显示的是而不是ó.该字符串来自从服务器检索的json文件.以下是技术细节:

I am having problem displaying accented character in my app; It is showing instead of ó. The string is coming from a json file retrieved from a server. Here are the technical details:

JSON:(这是从服务器中检索到的对象)
请注意第三个键Relación"的字母为"o".

JSON: (This is the object being retrieved from the server)
notice the 3rd key "Relación" the letter "o" is accented.

[
    {
        "key": "Canales"
    },
    {
        "key": "Productos"
    },
    {
        "key": "Relación con el ejecutivo"
    }
]

Ajax (这是用于检索信息的代码)
注意,我已经按照大多数答案的建议添加了charset = utf-8

Ajax (here is the code to retrieve the information)
notice I already added charset=utf-8 as most answers suggest

$.ajax({
    url: url,
    type: "GET",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(uri){
         alert("clintg test: " + JSON.stringify(uri));
    }
}

警告:(如您所见,它只是在应该位于的位置显示框符号-> ó)

Alert: (as you can see, it is just showing a box symbol where it's supposed to be -> ó)

推荐答案

要详细介绍@georg的帮助我解决此问题的建议:

To give more detail to @georg 's advice that helped me solve this issue:

由于我无法更改服务器端脚本,因此我调整了代码.
我将ajax请求中的字符集更改为 ISO-8859-1 ,但是由于ajax的默认字符集为utf-8,因此我不得不覆盖该字符集与$.ajax.beforeSend:

Since I can't change the server side scripts, I adjusted the code on my side.
I changed the charset in my ajax request to ISO-8859-1, but since the default charset of ajax is utf-8, I had to override the charset with $.ajax.beforeSend:

$.ajax({
    url: url,
    type: "GET",
    dataType: "json",
    contentType: "application/json; charset=iso-8859-1",
    success: function(uri){
         alert("clintg test: " + JSON.stringify(uri));
    },
    beforeSend: function(jqXHR) {
        jqXHR.overrideMimeType('application/json;charset=iso-8859-1');
    }
}

以下是该问题的链接,该问题帮助我找出并覆盖了字符集: Jquery忽略编码ISO-8859-1

Here's a link to the question that helped me figure out and override the charset: Jquery ignores encoding ISO-8859-1

这篇关于在JavaScript中显示带重音符号的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:49