$.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. iText操作word文档总结

    操作word文档的工具有很多,除了iText之外还有POI,但是POI擅长的功能是操作excel,虽然也可以操作word,但是能力有限,而且还有很多的bug,技术并不成熟,下面就重点介绍一种操作wor ...

  2. GEF的MVC体系结构

    摘要: 本文首先介绍了标准的 MVC 体系构架,同时也介绍了最常见的一类 MVC 模式的变种.之后,文章重点介绍了 MVC 结构在 gef 框架中的体现与应用,以及 gef 是如何综合利用工厂模式.命 ...

  3. 【足迹C++primer】40、动态数组

    动态数组 C++语言定义了第二种new表达式语法.能够分配并初始化一个对象数组.标准库中包括 一个名为allocator的类.同意我们将分配和初始化分离. 12.2.1 new和数组 void fun ...

  4. PHP移动互联网开发笔记(2)——变量及常量

    原文地址:http://www.php100.com/html/php/rumen/2014/0326/6703.html 一.PHP5.4的基本的语法格式 1.PHP的切割符 view source ...

  5. cocos2d-x 3.2 它 三消游戏——万圣节大作战

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

  6. windows phone 获取手机图片库中图片(4)

    原文:windows phone 获取手机图片库中图片(4) 前置条件:手机和电脑未连接或连接电脑Zune软件关闭(与Zune软件连接时不允许访问图片库): 版本7.1 获取手机图片库图片的两种方式: ...

  7. CC2530 外部中断 提醒

    #include "ioCC2530.h" #define uchar unsigned char #define led1    P1_0 #define led2    P1_ ...

  8. C#属性总结

    1.私有字段和公有字段的比较 类中的私有字段只能在类的内部访问,而对类的公有字段的访问却不受限制. 在以前,为了封装,程序中会尽量少使用公有字段,因为使用公有字段的话,会让程序失去控制权.所以程序中会 ...

  9. Just4Fun - Comparaison between const and readonly in C#

    /* By Dylan SUN */ Today let us talk about const and readonly. const is considered as compile-time c ...

  10. http1.0 和 http1.1 区别

    http1.0 和 http1.1 主要区别 1.背景   KeepAlive是就是通常所称的长连接.KeepAlive带来的好处是可以减少tcp连接的开销,这对于短response body的请求效 ...