java – jQuery AJAX调用混乱字符编码

我有一个输出 JSON

servlet. servlet的输出编码是ISO-8859-1.我们的webapp中的页面也设置为ISO-8859-1.我会使用UTF-8,但这不在我的控制之内;我们必须使用ISO-8859-1.

当我单独点击servlet时,我可以看到已经输出的JSON数据.字符编码是正确的,没有一个字符看起来很奇怪.

但是,当我通过AJAX调用servlet并使用检索到的数据来填充选择框时,我得到了(似乎)所有具有重音符号的字符(例如我带有重音或重音,音乐或抑扬音) ).当我在Firebug下的Net选项卡中查看响应时,我可以看到文本看起来很好.但是,当我使用该数据填充选择框时,我会得到带有问号的钻石.

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

编辑

更多信息.我在jQuery.ajax中使用GET,并将scriptCharset设置为ISO-8859-1.在服务器端,我使用request.setCharacterEncoding(“ISO-8859-1”)明确地将编码设置为ISO-8859-1;

编辑

代码示例:

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

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

我的servlet使用org.json.JSONObject,只需通过执行response.getWriter()输出字符串.print(jsonObject.toString());

UPDATE

根据关于JSON的评论以及它应该如何是UTF-8,我试着看看我是否可以将数据作为文本获取(所以将dataType设置为jQuery.ajax中的文本),然后将其评估为JSON(在Javascript中).这似乎也不起作用!当我做console.log时,我仍然得到了时髦的钻石.但是,当我在Firebug的Net选项卡下查看它时,一切都显示正常:

网络标签:

{"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)时,我得到以下结果:

{"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正在做一些奇怪的数据.

我终于弄明白了.这很奇怪!

response.setCharacterEncoding(String)不起作用(不知道它是否与我的设置有关或什么).看起来它设置了字符编码,但由于某种原因,jQuery将它全部搞砸了.您可以像这样显式设置标题

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

谢谢各位的帮助!

编辑

我做了一些研究并检查了 JavaDocs
并看到了这个:

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

.

所以上面仍然有效,但你也可以(也可能应该)这样做:

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

翻译自:https://stackoverflow.com/questions/3198532/jquery-ajax-call-messes-up-character-encoding

原文 

https://codeday.me/bug/20190113/527325.html

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » java – jQuery AJAX调用混乱字符编码

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址