有运营同学反映,后台编辑的一个中文显示名称,前台乱码了,于是乎~~

先看代码是否get请求没转码:

[javascript] view plaincopy
  1. $.ajax({
  2. type: 'POST',
  3. url: '/admin/updatedisplayname}',
  4. data: {displayName:displayName},
  5. success: function(res){
  6. alert(1);
  7. },
  8. error: function() {
  9. alert(2);
  10. },
  11. dataType: 'json'
  12. })

这段代码不管怎么看,也没有问题,post请求过去的,应该不会存在乱码问题,自己测了下,修改回去了,没乱码(火狐)

奇怪~

问了下,对方用的是谷歌,检查他的浏览器编码有没特殊设置过,没有,一切正常。

纳闷,回来在谷歌测试下,晕死,果然重现,乱码了,哈哈,那就好办,查~

看看请求头信息啥的,发现使用$.ajax请求的时候有一个值有问题

chrome:contentType: 'application/x-www-form-urlencoded'

ff:contentType: 'application/x-www-form-urlencoded; charset=UTF-8',

难道是这个在作祟,如果乎~

[javascript] view plaincopy
  1. $.ajax({
  2. type: 'POST',
  3. url: '/admin/updatedisplayname}',
  4. data: {displayName:displayName},
  5. contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  6. success: function(res){
  7. alert(1);
  8. },
  9. error: function() {
  10. alert(2);
  11. },
  12. dataType: 'json'
  13. })

加上参数测试,果然在谷歌下不乱码了,问题解决。

莫不是有啥玄机,查看jQuery手册

contentTypeString

(默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型。默认值适合大多数情况。如果你明确地传递了一个content-type给 $.ajax() 那么他必定会发送给服务器(即使没有数据要发送)

为毛我在不传入这个值的时候,火狐会加上 charset=UTF-8呢?

看看jquery源码:

[javascript] view plaincopy
  1. ajaxSettings: {
  2. url: location.href,
  3. global: true,
  4. type: "GET",
  5. contentType: "application/x-www-form-urlencoded",
  6. processData: true,
  7. async: true,
  8. /*
  9. timeout: 0,
  10. data: null,
  11. username: null,
  12. password: null,
  13. traditional: false,
  14. */
  15. // Create the request object; Microsoft failed to properly
  16. // implement the XMLHttpRequest in IE7 (can't request local files),
  17. // so we use the ActiveXObject when it is available
  18. // This function can be overriden by calling jQuery.ajaxSetup
  19. xhr: window.XMLHttpRequest && (window.location.protocol !== "file:" || !window.ActiveXObject) ?
  20. function() {
  21. return new window.XMLHttpRequest();
  22. } :
  23. function() {
  24. try {
  25. return new window.ActiveXObject("Microsoft.XMLHTTP");
  26. } catch(e) {}
  27. },
  28. accepts: {
  29. xml: "application/xml, text/xml",
  30. html: "text/html",
  31. script: "text/javascript, application/javascript",
  32. json: "application/json, text/javascript",
  33. text: "text/plain",
  34. _default: "*/*"
  35. }
  36. },

貌似问题已经找到,通知团队成员,仔细检查所有项目的$.ajax方法的使用,是否有传递中文参数,如果有就加上:

[javascript] view plaincopy
  1. contentType: 'application/x-www-form-urlencoded; charset=UTF-8',

果然发现了几个隐藏的bug,不过像这样的直接使用$.ajax一般只有后台系统才用用到,而我们的后台系统是可以不兼容google的,所以当时测试的时候可能没测到。

不过,又有问题,有同学没有加,但是测试是ok的,谷歌不乱码,各种不淡定啊。。。。

再查~

首先想到检查jquery版本,果然,这哥们是用jquery1.7.2,而我用的是1.4.2,查看1.7.2的源码:

[javascript] view plaincopy
  1. ajaxSettings: {
  2. url: ajaxLocation,
  3. isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ),
  4. global: true,
  5. type: "GET",
  6. contentType: "application/x-www-form-urlencoded; charset=UTF-8",
  7. processData: true,
  8. async: true,
  9. /*
  10. timeout: 0,
  11. data: null,
  12. dataType: null,
  13. username: null,
  14. password: null,
  15. cache: null,
  16. traditional: false,
  17. headers: {},
  18. */
  19. accepts: {
  20. xml: "application/xml, text/xml",
  21. html: "text/html",
  22. text: "text/plain",
  23. json: "application/json, text/javascript",
  24. "*": allTypes
  25. },
  26. contents: {
  27. xml: /xml/,
  28. html: /html/,
  29. json: /json/
  30. },
  31. responseFields: {
  32. xml: "responseXML",
  33. text: "responseText"
  34. },
  35. // List of data converters
  36. // 1) key format is "source_type destination_type" (a single space in-between)
  37. // 2) the catchall symbol "*" can be used for source_type
  38. converters: {
  39. // Convert anything to text
  40. "* text": window.String,
  41. // Text to html (true = no transformation)
  42. "text html": true,
  43. // Evaluate text as a json expression
  44. "text json": jQuery.parseJSON,
  45. // Parse text as xml
  46. "text xml": jQuery.parseXML
  47. },
  48. // For options that shouldn't be deep extended:
  49. // you can add your own custom options here if
  50. // and when you create one that shouldn't be
  51. // deep extended (see ajaxExtend)
  52. flatOptions: {
  53. context: true,
  54. url: true
  55. }
  56. },

晕死,在1.7.2中居然加上了charset=UTF-8,

各种~~~~,再探~1.7源码里面也是没有的

好吧,1.7.2里面没有这个问题。

结论:

1、使用1.7.2之前的版本,在使用$.ajax的时候需要自行设置contentType否则,谷歌会乱码。

2、跟浏览器也有关系,火狐就可以自动加上utf-8而谷歌则不会,也行jquery团队发现这个问题,所以就在最新版更正了这个问题。

测试及生成环境:

1、Java环境,打包gbk,页面编码gbk,tomcat中URIEncoding配置utf-8等,也许应该跟这些服务器配置也是有关系的,就不深究下去了。

猜测:一般post表单请求的请求数据编码是按照页面里面的meta指定的编码格式编码的,我们页面是gbk的,所以在谷歌浏览器在未指定ajax的contentType参数的时候采用默认页面编码方式gbk编码然后发送到服务器端,而我们tomcat里面的URIEncoding是utf-8的,所以就两边编码不一样,就乱码了,显式的指定一下就ok。不过貌似ajax统一都是utf-8的来编码的,就算不指定也不会有问题,莫不是谷歌。。。。

最新文章

  1. 干货|最全面的卷积神经网络入门教程
  2. JavaWeb学习中的小问题
  3. Vue keep-alive实践总结
  4. ubuntu16.04中文乱码解决方案
  5. 四周第五次课(1月6日) 6.5 zip压缩工具 6.6 tar打包 6.7 打包并压缩
  6. 量化策略回测ocobreak
  7. 改进的冒泡排序算法一
  8. pytorch test单张图片_PyTorch的元学习库:Torchmeta
  9. 商城管理系统源码 商城APP源码 电子商城源码
  10. 网络攻防技术——环境变量与set-uid实验
  11. css2仿微信导航栏-滑动门
  12. 合并Windows系统镜像教程
  13. Ask and Answer
  14. 如何深入理解php中的值传递和引用传递
  15. ip 地址在线解析, api
  16. 08.零售类公司分析•上
  17. lgy -oracle
  18. 数据分析师需要学什么?数据分析师需要掌握什么技能呢?
  19. 紫晶存储研发核心成员离职,不受影响是真的么?
  20. Build Library By xcodebuild

热门文章

  1. HDU:1222wolfnbsp;andnbsp;habbit解题报告
  2. python木马病毒_Python引导木马病毒(拓展篇)
  3. 临沂一中高考2021成绩查询,2021年临沂高考状元名单公布,临沂高考状元学校资料及最高分...
  4. 求职互联网技术岗应届生面试必备技巧分享
  5. 辨识DV、OV、EV三种证书类型
  6. 【北京线下】FMI2018人工智能大数据技术沙龙第869期
  7. chm 已取消到该网页的导航,打不开
  8. Silverlight智能表单(2)从工具箱到画板
  9. 一文读懂什么是数据产品交易
  10. 12306余票查询(六)——优化页面结构,加入js请求数据部分