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

摘要:
代码如下:$。ajax({dataType:'json',type:'POST',url:'http://localhost/test/test.do',数据:{id:1,类型:“商品”},成功:函数(数据){}});问题:提交后端操作程序时,获得的类型是乱码。解决方案:方法1:在提交之前使用encodeURI编码两次。记住必须是两次。1.修改以下代码副本
代码如下:

$.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方式
服务器端java代码:
复制代码 代码如下:

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

客户端js代码:
复制代码 代码如下:

$.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方式
服务器端java代码:
复制代码 代码如下:

request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");

客户端js代码:
复制代码 代码如下:

$.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()函数传值中文乱码解决方法介绍》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇powerdesigner逆向工程生成PDM时的列注释解决方案的补充练习2-10 计算分段函数[1] (10 分)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ajax(省,市,县)三级联动

下面我们用Jquery,ajax,做一个省,市,县的三级联动: 下面是我做三级联动下拉的步骤以及逻辑 第一步:先做一个省市区表格 第二步:建个PHP页面显示用我是在<body>里放<div>用来接收要显示的省市区表格信息,里面嵌入jquery-1.11.2.min.js和自己封装的三联动省市区的方法 第三步:写封装方法用JS 第四...

umi-request 一个新的请求工具

简单封装 npm install umi-request --save 文档,具体封装可以多看看文档配置 import { extend } from "umi-request"; const request = extend({ prefix: "xxx",//相当于baseurl timeout: 10000, errorHandle...

第一百八十节,jQuery-UI,知问前端--消息提示 UI

jQuery-UI,知问前端--消息提示 UI 学习要点:   1.HTML 部分   2.CSS 部分   3.jQuery 部分 通过前面已学的 jQuery UI 部件,我们来创建一个注册表单。 html <div id="reg" title="会员注册"> <p> <label fo...

深入剖析jsonp跨域原理

在项目中遇到一个jsonp跨域的问题,于是仔细的研究了一番jsonp跨域的原理。搞明白了一些以前不是很懂的地方,比如: 1)jsonp跨域只能是get请求,而不能是post请求; 2)jsonp跨域的原理到底是什么; 3)除了jsonp跨域之外还有那些方法绕过“同源策略”,实现跨域访问; 4)jsonp和ajax,或者说jsonp和XMLHttpReque...

JAVA 调用HTTP接口POST或GET实现方式(转)

HTTP是一个客户端和服务器端请求和应答的标准(TCP),客户端是终端用户,服务器端是网站。通过使用Web浏览器、网络爬虫或者其它的工具,客户端发起一个到服务器上指定端口(默认端口为80)的HTTP请求。 具体POST或GET实现代码如下: packagecom.yoodb.util; importjava.io.ByteArrayOutputStre...

flask中使用ajax 处理前端POST请求 弹框展示

菜小鱼初次使用 ajax,想前端提交数据,后端处理后,将结果以弹框的形式展示,在网上查看了好多,不停的调试,终于调通了 html: 1 <html> 2 <head></head> 3 <body> 4 <form class="formXXX1" method="post"> 5 &l...