jquery之ajax中国乱码的解决方案
$.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中国乱码的解决方案的更多相关文章
- JQuery在Ajax的Post提交中国乱码的解决方案
介绍: 在JQuery的Ajax POST要求,一个要求.中国的背景之中,乱码,如何解决呢? 问题简介: var regid = $('#oregion').combobox('getValue'); ...
- burp suite中国乱码的解决方案
于http的response还有经常出现乱码,其实解决的办法很easy 首先点击Options标签, 然后找到display 找到http message display watermark/2/te ...
- JQuery中Ajax的Post提交中文乱码的解决方案
出自:http://m.blog.csdn.net/blog/blueheart20/26370023 引言: 在JQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何 ...
- 在jQuery中Ajax的Post提交中文乱码的解决方案
引言: 在jQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何解决呢? 问题的引入: var regid = $('#oregion').combobox('getV ...
- jquery的ajax()函数中文传值出现乱码完美解决方案
1. jquery的ajax()函数 $.ajax({ type: "POST", dataType: "text", url: ".. ...
- jquery的ajax()函数传值中文乱码解决方法介绍
jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...
- [转载]JQuery的Ajax跨域请求的解决方案
今天在项目中需要做远程数据加载并渲染页面,直到开发阶段才意识到ajax跨域请求的问题,隐约记得Jquery有提过一个ajax跨域请求的解决方式,于是即刻翻出Jquery的API出来研究,发现JQuer ...
- JQuery中Ajax的Post提交在IE下中文乱码的解决方法
原文地址:http://www.bitscn.com/pdb/ajax/316671.html 引言: 在JQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何解决呢? ...
- jQuery AJAX中文乱码处理
最近工作中用jQuery ajax返回出现乱码,用的Notepad++编辑器,当JS部分传递中文时,另一页面接收的话会出现乱码,在网上找了很多方法,基本上没有很好的解决. 页面用GB2312编码,JS ...
随机推荐
- 【转】真正的Acmer
上海交大 戴文渊 大牛写的东西,建议大家看看 yiyiyi4321 2007-07-10 13:49:30.0 http://dwyak.spaces.live.com/?_c11_BlogPart_ ...
- 查看mysql一些命令的数据库状态
命令: show processlist; 假设是root帐号,你能看到全部用户的当前连接.假设是其他普通帐号,仅仅能看到自己占用的连接. show processlist;仅仅列出前100条, ...
- 《Android内核剖析》读书笔记 第13章 View工作原理【View重绘过程】
计算视图大小的过程(Measure) 视图大小,准确的来说应该是指视图的布局大小:我们在layout.xml中为每个UI控件设置的layout_width/layout_height两个属性被用来设置 ...
- java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ThreadPoolExecutor An ExecutorService that executes each submitted task using o ...
- HDU1071 The area 【积分】
The area Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- Android编程获取网络连接状态及调用网络配置界面
获取网络连接状态 随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityMan ...
- android笔记6——intent的使用
今天挑出一节专门来说一下使用intent和intentfilter进行通信. 场景:一个Activity启动还有一个Activity. 前面已经讲了Fragment的切换,Fragment顾名思义是基 ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
1.错误叙述性说明 2014-7-10 23:12:23 org.apache.catalina.core.StandardContext filterStart 严重: Exception star ...
- 排列组合相关算法 python
获取指定长度得全部序列 通过事件来表述这个序列,即n重伯努利实验(二项分布)的全部可能结果.比如时间a表示为: a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 假设每次实验为从 ...
- 《反project核心原则》说明
致亲爱的中国读者: 大家好 !我是<逆向project核心原理> 作者 李承远(ReverseCore). (韩文博客地址:www.reversecore.com) 首先.非常高兴我的 ...