内容简介:翻译自:https://stackoverflow.com/questions/3198532/jquery-ajax-call-messes-up-character-encoding
的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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 直观讲解-RPC调用和HTTP调用的区别
- 调用链系列一:解读UAVStack中的调用链技术
- 调用链系列二:解读UAVStack中的调用链技术
- 调用链系列三:解读UAVStack中的调用链技术
- dubbo源码解析(二十七)远程调用——injvm本地调用
- 微服务间的调用和应用内调用有什么区别
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。