jquery 乱码

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

星期二, 五月 22nd, 2012 | ajax, Android, html5 | 一条评论

在用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: , ,

Search

文章分类

Links

Meta