jquery提交post数据在firefox正常在chrome和ie下乱码解决

星期二, 2012-05-22 | Author: Lee | ajax, Android, html5 | 11,070 views

在用jquery处理html5的应用的时候,一直在firefox下测试都正常,用户用pad访问的时候说有乱码,
自己试验了下果然,后发现chrome和ie内核下都是有此问题,此问题设置了页面属性为utf-8时候,只有firefox是传的charset=utf-8的头文件
chrome和ie都没有指定,所以出现乱码问题.
解决方法:

$.ajaxSetup({
  contentType: "application/x-www-form-urlencoded; charset=utf-8"
});
$.post("test.php", { name: "i5a6", time: "2pm" },
   function(data){
     process(data);
   }, "json");

或者使用:

$.ajax({
  url:url,
  type:"POST",
  data:data,
  contentType:"application/x-www-form-urlencoded; charset=utf-8",
  dataType:"json",
  success: function(){
    ...
  }
})

推荐使用第一种,不过也是根据自己的实际情况看的,有人推荐用 encodeURIComponent 做字符转换,不推荐.

Tags: , ,

文章作者: Lee

本文地址: https://www.pomelolee.com/973.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

一条评论 to jquery提交post数据在firefox正常在chrome和ie下乱码解决

Kayo
2012 年 05 月 23 日

再次显示了 jQuery 的方便之处,要是原生js就麻烦了!

Leave a comment

Search

文章分类

Links

Meta