$.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. gustafson,Sun-Ni,Amdahl

    gustafson 定律由 John Gustafson首先提出.描述:系统优化某部件所获得的系统性能的改善程度,取决于该部件被使用的频率,或所占总执行时间的比例. Gustafson定理中,加速比与 ...

  2. Hibernate常用Annotation标签说明

    @ javax.persistence.Entity 实体类定义,该标签表示当前类是一个Hibernate的数据库实体,对应着数据库中的某个表 位置:用于类级别 参数:无 样例:@Entity 注意: ...

  3. 我写了一起 Makefile(一)

    我写了一起 Makefile  陈皓 概述—— 什么是makefile?也许非常多Winodws的程序猿都不知道这个东西,由于那些Windows的IDE都为你做了这个工作.但我认为要作一个好的和pro ...

  4. 一旦ORA-28000: the account is locked用户锁定故障排除

    今天我的同事们反映的问题,测试库的变化password,并改变相关的应用程序中使用password后,其中一个仍然会出现在帐户被锁定,报告ORA-28000: the account is locke ...

  5. [Network]Application Layer

    1 Principles of Network Applications 1.1 Application Architectures Client-Server Peer-to-Peer Hybird ...

  6. 《Effective C++ 》学习笔记——规定10

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  7. JQuery打印

    jquery.jqprint-0.3.js JQuery提供的局部打印功能: <input type="button" value="打印" onclic ...

  8. C++习题 对象转换

    [Submit][Status][Web Board] Description 定义一个Teacher(教师)类(教师号,姓名,性别,薪金)和一个Student(学生)类(学号,姓名,性别,成绩),二 ...

  9. SpringMVC 上下文webApplicationContext

    使用listener听众载入配置,一般Struts+Spring+Hibernate是使用listener监听器的.例如以下 <listener> <listener-class&g ...

  10. [置顶] ios 一个不错的图片浏览分享框架demo

    demo功能:一个不错的图片浏览分享框架demo.iphone6.1 测试通过.可以浏览图片,保存,微博分享到新浪,腾讯,网易,人人等. 注:(由于各个微博的接口有时候会有调整,不一定能分享成功.只看 ...