jQuery AJAX调用弄乱了字符编码 [英] jQuery AJAX call messes up character encoding

查看:91
本文介绍了jQuery AJAX调用弄乱了字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输出JSON的servlet. Servlet的输出编码为ISO-8859-1.我们的Web应用程序中的页面也设置为ISO-8859-1.我会使用UTF-8,但这超出了我的控制范围.我们必须使用ISO-8859-1.

I have a servlet that outputs JSON. The output encoding for the servlet is ISO-8859-1. Pages in our webapp are also set to ISO-8859-1. I would use UTF-8, but this is outside my control; we have to use ISO-8859-1.

当我自己点击servlet时,我可以看到已输出的JSON数据.字符编码正确,而且所有字符看起来都不奇怪.

When I hit the servlet by itself, I can see JSON data that has been outputted. The character encoding is correct, and none of the characters look strange.

但是,当我通过AJAX调用servlet并使用检索到的数据填充选择框时,我得到了-似乎代替了所有带有重音符号的字符(例如,我带有重音符或尖锐重音符号,diresis) ,或抑扬音符号).当我在Firebug下的网络"选项卡中查看响应时,可以看到文本看起来不错.但是,当我使用该数据填充选择框时,会出现带问号的菱形.

However, when I call the servlet via AJAX and use the data retrieved to populate a select box, I get � in the place of (it seems) all characters that have accents (for example i with grave or acute accent, dieresis, or circumflex). When I look at the response in the Net tab under Firebug, I can see that that the text looks fine. However, when I use that data to populate the select box, I get the diamond-with-questionmark.

这些字符都是有效的ISO-8859-1字符,所以我不明白为什么它们不能正确显示.

These characters are all valid ISO-8859-1 characters, and so I don't understand why they don't show up correctly.

编辑

更多信息.我在jQuery.ajax中使用GET,并且已将scriptCharset设置为ISO-8859-1.在服务器端,我已使用request.setCharacterEncoding("ISO-8859-1");

Some more information. I use GET in jQuery.ajax and I've set scriptCharset to ISO-8859-1. On the server-side, I've explicitly set the encoding to ISO-8859-1 using request.setCharacterEncoding("ISO-8859-1");

编辑

代码示例:

这是我目前所拥有的.我添加了scriptCharset: "ISO-8859-1"无效.

This is what I have currently. I added scriptCharset: "ISO-8859-1" to no effect.

        jQuery.ajax({
            url: "/countryAndProvinceCodeServlet",
            data: data,
            dataType: "json",
            type: "GET",
            success: function(data) {
               ...
            },
        });

我的servlet使用org.json.JSONObject并通过执行response.getWriter().print(jsonObject.toString());

My servlet uses org.json.JSONObject and simply outputs the string by doing response.getWriter().print(jsonObject.toString());

更新

根据有关JSON及其应为UTF-8的注释,我尝试查看是否可以将数据抓取为文本(因此在jQuery.ajax中将dataType设置为text),然后将其评估为JSON我自己(使用Javascript).这似乎也不起作用!当我执行console.log时,我仍然得到时髦的钻石.但是,当我在Firebug的网络"标签下查看它时,一切都很好显示:

Per the comments about JSON and how it should be UTF-8, I tried to see if I could grab the data as text (so set dataType to text in jQuery.ajax) and then evaluate it as JSON myself (in Javascript). That doesn't seem to work either! When I do console.log, I still get the funky diamonds. However, when I look at it under the Net tab in Firebug everything shows up fine:

网络"标签:

{"error":false,
 "provinces":{"DZ-01":"Adrar",
              "DZ-16":"Alger",
              "DZ-23":"Annaba",
              "DZ-44":"Aïn Defla",
              "DZ-46":"Aïn Témouchent",
              "DZ-05":"Batna",
              "DZ-07":"Biskra",
              "DZ-09":"Blida",
              "DZ-34":"Bordj Bou Arréridj",
              "DZ-10":"Bouira",
              "DZ-35":"Boumerdès",
              "DZ-08":"Béchar",
              "DZ-06":"Béjaïa",
              "DZ-02":"Chlef",
              "DZ-25":"Constantine",
              "DZ-17":"Djelfa",
              "DZ-32":"El Bayadh",
              "DZ-39":"El Oued",
              "DZ-36":"El Tarf",
              "DZ-47":"Ghardaïa",
              "DZ-24":"Guelma",
              "DZ-33":"Illizi",
              "DZ-18":"Jijel",
              "DZ-40":"Khenchela",
              "DZ-03":"Laghouat",
              "DZ-29":"Mascara",
              "DZ-43":"Mila",
              "DZ-27":"Mostaganem",
              "DZ-28":"Msila",
              "DZ-26":"Médéa",
              "DZ-45":"Naama",
              "DZ-31":"Oran",
              "DZ-30":"Ouargla",
              "DZ-04":"Oum el Bouaghi",
              "DZ-48":"Relizane",
              "DZ-20":"Saïda",
              "DZ-22":"Sidi Bel Abbès",
              "DZ-21":"Skikda",
              "DZ-41":"Souk Ahras",
              "DZ-19":"Sétif",
              "DZ-11":"Tamanghasset",
              "DZ-14":"Tiaret",
              "DZ-37":"Tindouf",
              "DZ-42":"Tipaza",
              "DZ-38":"Tissemsilt",
              "DZ-15":"Tizi Ouzou",
              "DZ-13":"Tlemcen",
              "DZ-12":"Tébessa"}}

但是,当我对jQuery.ajax中的内容执行console.log(text)时,会得到以下信息:

But when I do console.log(text) with what I get from jQuery.ajax, I get the following:

{"error":false,
 "provinces":{"DZ-01":"Adrar",
              "DZ-16":"Alger",
              "DZ-23":"Annaba",
              "DZ-44":"A�n Defla",
              "DZ-46":"A�n T�mouchent",
              "DZ-05":"Batna",
              "DZ-07":"Biskra",
              "DZ-09":"Blida",
              "DZ-34":"Bordj Bou Arr�ridj",
              "DZ-10":"Bouira",
              "DZ-35":"Boumerd�s",
              "DZ-08":"B�char",
              "DZ-06":"B�ja�a",
              "DZ-02":"Chlef",
              "DZ-25":"Constantine",
              "DZ-17":"Djelfa",
              "DZ-32":"El Bayadh",
              "DZ-39":"El Oued",
              "DZ-36":"El Tarf",
              "DZ-47":"Gharda�a",
              "DZ-24":"Guelma",
              "DZ-33":"Illizi",
              "DZ-18":"Jijel",
              "DZ-40":"Khenchela",
              "DZ-03":"Laghouat",
              "DZ-29":"Mascara",
              "DZ-43":"Mila",
              "DZ-27":"Mostaganem",
              "DZ-28":"Msila",
              "DZ-26":"M�d�a",
              "DZ-45":"Naama",
              "DZ-31":"Oran",
              "DZ-30":"Ouargla",
              "DZ-04":"Oum el Bouaghi",
              "DZ-48":"Relizane",
              "DZ-20":"Sa�da",
              "DZ-22":"Sidi Bel Abb�s",
              "DZ-21":"Skikda",
              "DZ-41":"Souk Ahras",
              "DZ-19":"S�tif",
              "DZ-11":"Tamanghasset",
              "DZ-14":"Tiaret",
              "DZ-37":"Tindouf",
              "DZ-42":"Tipaza",
              "DZ-38":"Tissemsilt",
              "DZ-15":"Tizi Ouzou",
              "DZ-13":"Tlemcen",
              "DZ-12":"T�bessa"}}

在我看来,jQuery在处理数据方面有些奇怪.

It seems to me that jQuery is doing something weird with the data.

推荐答案

我终于知道了.很奇怪!

I finally figured it out. It's pretty weird!

response.setCharacterEncoding(String)不能不起作用(不知道它是否与我的设置有关或与我有关).看起来它设置了字符编码,但是由于某种原因,jQuery搞砸了所有字符.您已经明确设置了标题,如下所示:

response.setCharacterEncoding(String) does not work (don't know if it's related to my setup or what). It looks like it sets the character encoding, but for some reason jQuery messes it all up. You have the explicitly set the headers like so:

response.setHeader("Content-Type", "application/json; charset=ISO-8859-1");

谢谢大家的帮助!

编辑

我做了一些研究,并检查了

I did some research and checked out the JavaDocs and saw this:

如果协议提供了一种方法,则容器必须将用于Servlet响应的编写器的字符编码传达给客户端.在HTTP的情况下,字符编码作为文本媒体类型的Content-Type标头的一部分进行通信. 请注意,如果servlet未指定内容类型,则不能通过HTTP标头传达字符编码 ;但是,它仍然用于对通过Servlet响应的writer编写的文本进行编码.

Containers must communicate the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the Content-Type header for text media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the servlet response's writer.

因此上面的方法仍然有效,但是您也可以(并且应该这样做):

So the above still works, but you can also (and probably should) do this:

response.setContentType("application/json");
response.setCharacterEncoding("ISO-8859-1"); 

这篇关于jQuery AJAX调用弄乱了字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆