$.ajax({

  dataType : ‘json',type : ‘POST',url : ‘http://localhost/test/test.do',data : {id: 1, type: ‘商品'},success : function(data){ } } );


问题:

提交后后台action程序时,取到的type是乱码

解决方法:

方法一:提交前採用encodeURI两次编码,记住一定是两次

1.改动下面代码



data:{id:1, type:encodeURI(encodeURI(‘商品'))}

2.在后台action里要对取得的字符串进行decode

1、String type = request.getParameter(“type”);

2、type = URLDecoder.decode(type, “UTF-8″);

方法二:ajax配置contentType属性,加上charset=UTF-8

在ajax方法中增加下面參数

contentType: “application/x-www-form-urlencoded; charset=UTF-8″使用其他js框架或者xhr都是差点儿相同,设置header中contentType就可以,


这里关键是charset=UTF-8,假设没有这个,是不行的,默认jQuery里的contentType是没有的



一、測试环境

jQuery:1.3.2

tomcat:5.5.17

二、測试方法

1.使用get方式

server端java代码:



String name = new String(request.getParameter("name").getBytes("iso8859-1"),"utf-8");

clientjs代码:



$.ajax({url: "2.jsp",type: "get",data: {name:"中文"},success: function(response){


alert(response);

}});

结果:正确显示



$.ajax({url: "2.jsp",type: "get",data: "name=中文",success: function(response){


alert(response);

}});

结果:乱码



$.get("2.jsp", { name: "中文" },function(response){


alert(response);

});

结果:正确显示

复制代码 代码例如以下:


$.get("2.jsp", "name=中文",function(response){


alert(response);

});

结果:乱码



2.post方式

server端java代码:



request.setCharacterEncoding("UTF-8");


String name = request.getParameter("name");

clientjs代码:



$.ajax({url: "3.jsp",type: "post",data: "method=testAjaxPost&name=中文",success: function(response){


alert(response);

}});

结果:正确显示

$.ajax({url: "3.jsp",type: "post",data: {name:"中文"},success: function(response){

alert(response);

}});



结果:正确显示



$.post("3.jsp", { name: "中文" },function(response){


alert(response);

});

结果:正确显示



$.post("3.jsp", "name=中文",function(response){


alert(response);

});

结果:正确显示

三、使用filter



public void doFilter(ServletRequest request, ServletResponse response,


FilterChain chain) throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;

if (req.getHeader("X-Requested-With") != null && req.getHeader("X-Requested-With").equalsIgnoreCase("XMLHttpRequest")) {


request.setCharacterEncoding("utf-8");

} else {

request.setCharacterEncoding("gbk");

}

chain.doFilter(request, response);

}

jQuery在使用ajax的时候会在header中增加X-Requested-With,值为:XMLHttpRequest,filter中推断是jQuery的ajax请求时就把字符编码设为utf8,这样能够解决post提交中的中文乱码问题,不须要在代码中设置request.setCharacterEncoding("UTF-8");


对于get方式的中文乱码问题。建议不使用get方式提交中文。统统改为post ^-^



为了和prototype.js处理中文的方式一致,能够使用例如以下的方式,自己定义header中的属性RequestType



$.ajax({

url: "3.jsp",

type: "post",

data: {name:"中文"},

beforeSend: function(XMLHttpRequest){

XMLHttpRequest.setRequestHeader("RequestType", "ajax");

alert("開始");

},

success: function(data, textStatus){

alert(data);

},

error: function(XMLHttpRequest, textStatus, errorThrown){

alert("错误:" + textStatus);

},

complete: function(XMLHttpRequest, textStatus){

alert("完毕:" + textStatus);

}

});

filter代码例如以下:



public void doFilter(ServletRequest request, ServletResponse response,


FilterChain chain) throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;

if (req.getHeader("RequestType") != null && req.getHeader("RequestType").equalsIgnoreCase("ajax"))) {


request.setCharacterEncoding("utf-8");

} else {

request.setCharacterEncoding("gbk");

}

chain.doFilter(request, response);

}

版权声明:本文博主原创文章,博客,未经同意不得转载。

jquery之ajax中国乱码的解决方案的更多相关文章

  1. JQuery在Ajax的Post提交中国乱码的解决方案

    介绍: 在JQuery的Ajax POST要求,一个要求.中国的背景之中,乱码,如何解决呢? 问题简介: var regid = $('#oregion').combobox('getValue'); ...

  2. burp suite中国乱码的解决方案

    于http的response还有经常出现乱码,其实解决的办法很easy 首先点击Options标签, 然后找到display 找到http message display watermark/2/te ...

  3. JQuery中Ajax的Post提交中文乱码的解决方案

    出自:http://m.blog.csdn.net/blog/blueheart20/26370023 引言: 在JQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何 ...

  4. 在jQuery中Ajax的Post提交中文乱码的解决方案

    引言: 在jQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何解决呢? 问题的引入: var regid = $('#oregion').combobox('getV ...

  5. jquery的ajax()函数中文传值出现乱码完美解决方案

    1.        jquery的ajax()函数 $.ajax({ type: "POST", dataType: "text", url: ".. ...

  6. jquery的ajax()函数传值中文乱码解决方法介绍

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...

  7. [转载]JQuery的Ajax跨域请求的解决方案

    今天在项目中需要做远程数据加载并渲染页面,直到开发阶段才意识到ajax跨域请求的问题,隐约记得Jquery有提过一个ajax跨域请求的解决方式,于是即刻翻出Jquery的API出来研究,发现JQuer ...

  8. JQuery中Ajax的Post提交在IE下中文乱码的解决方法

    原文地址:http://www.bitscn.com/pdb/ajax/316671.html 引言: 在JQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何解决呢? ...

  9. jQuery AJAX中文乱码处理

    最近工作中用jQuery ajax返回出现乱码,用的Notepad++编辑器,当JS部分传递中文时,另一页面接收的话会出现乱码,在网上找了很多方法,基本上没有很好的解决. 页面用GB2312编码,JS ...

随机推荐

  1. GString及IntelliJIdea中调试Groovy的操作步骤

    今天是学习Groovy的第一天,首先我觉得学习任何一种语言都要先弄清楚这种语言的特性,因为只有了解了特性之后学习才能达到好的效果,那么groovy的特点是什么的.我觉得groovy是一种动态语言,动态 ...

  2. java正則表達式的坑

    java中正則表達式比較有意思,这里列举几个常见的坑 1.[]符号,中括号表示当中的数据都是或的关系 假设[\\w+]是匹配条件 abc能否够匹配的到呢? 首先\\w(注意这里没有中括号)表示a-z ...

  3. iOS一些推荐的学习路径发展

    iOS论坛里有朋友要求回答帖子,帖子的标题是: 想学IOS开发高阶一点的东西,从何開始,然后我吧啦吧啦回答写了非常多.既然敲了那么多字,我就把我写的回复也贴到博客里来分享.希望能对大家有帮助.欢迎大家 ...

  4. ecshop首页调用某分类下的商品|assign_cat_goods()

    ecshop首页调用分类下的商品其实很简单,也有模板设置那里可以设置,不过那个只可以用cat_goods.lib,不方便,所以我想看看怎么能简单的实现ecshop首页调用分类下的商品 只需要在inde ...

  5. Tomcat7.0设置虚拟文件夹

    (1)眼下,我们的网站网站都是放在默认的文件夹下:tomcat/webapps/下的.可是,在某种情况下.我们须要把网站放到其它的文件夹,比方:tomcat所在磁盘的空间不足: 或者为了项目的统一管理 ...

  6. 全部编程皆为Web编程

    原文作者:Jeff Atwood   原文链接:http://blog.codinghorror.com/all-programming-is-web-programming Michael Brau ...

  7. [背景分离] 识别移动物体基于高斯混合 MOG

    使用很easy,  frame 就是当前帧,  foreground 是取得的, binary 型背景, 0.03是学习速率能够依据实际调整. cv::BackgroundSubtractorMOG ...

  8. hadoop调度程序时出现“Error opening job jar”错误

    提示出现的问题: Exception in thread "main" java.io.IOException: Error opening job jar: /home/depl ...

  9. mysql重装后出现乱码解决办法

    查看当前连接系统参数:SHOW VARIABLES LIKE '%char%'; mysql> show variables like 'char%'; +------------------- ...

  10. 一起学习android图片四舍五入图片集资源 (28)

    效果图: 參看下面代码: public class MainActivity extends Activity { private ImageView imageView1; private Imag ...