ajax()函数传值中文乱码解决方法介绍
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方式
服务器端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);
}
ajax()函数传值中文乱码解决方法介绍的更多相关文章
- jquery的ajax()函数传值中文乱码解决方法介绍
jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...
- ajax url参数中文乱码解决方法
较好的处理办法,对js的url中的中文参数值使用两次encodeURI(),即encodeURI(encodeURI("url的中文参数值")) JS代码: var name=&q ...
- php mysql 中文乱码解决方法
本文章向码农们介绍php mysql 中文乱码解决方法,对码农们非常实用,需要的码农可以参考一下. 从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行 解 ...
- Django 分页查询并返回jsons数据,中文乱码解决方法
Django 分页查询并返回jsons数据,中文乱码解决方法 一.引子 Django 分页查询并返回 json ,需要将返回的 queryset 序列化, demo 如下: # coding=UTF- ...
- Java 前台后台数据传递、中文乱码解决方法
1.向前台传递数据;2.向后台传递数据;3.ajax post 提交数据到服务端时中文乱码解决方法;4.数组类型参数传递; 1.向前台传递数据:1.1 字符串数据传递: 这种方式只是单一的向前台传递 ...
- [转]mysql导入导出数据中文乱码解决方法小结
本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...
- Zxing中文乱码解决方法
Zxing中文乱码解决方法总结 尝试过非常多方法 最后发现此方法解决的乱码最多....... 在百度搜索二维码图片 经过前2页的測试 除开一张图之外 其余都能扫描出结果 假设大家有更好的解决方法 ...
- unity3d 中文乱码解决方法——cs代码文件格式批量转化UTF8
在Unity3d中经常会碰到中文乱码的问题,比如代码中的[AddComponentMenu("GameDef/AI/战机AI")],注释,中文文本等等 其原因在于,unity本身是 ...
- Codeblocks中文乱码解决方法
odeblocks中文乱码解决方法: 特别提示:出现中文乱码情况才执行以下操作,未出现请勿随意修改!!!! 打开Codeblocks -> 设置 -> 编辑器: 然后点击 Encoding ...
随机推荐
- window下安裝redis服務
一.下载windows版本的Redis github下载地址:https://github.com/MicrosoftArchive/redis/releases/tag/win-3.2.100 ...
- python处理数据的风骚操作[pandas 之 groupby&agg]
https://segmentfault.com/a/1190000012394176 介绍 每隔一段时间我都会去学习.回顾一下python中的新函数.新操作.这对于你后面的工作是有一定好处的.本文重 ...
- JQuery小知识点代码
1.链式操作 $(function(){ /*var oDiv = $('#div1'); oDiv.html('hello'); oDiv.css('background','red'); oDiv ...
- POJ3275:Ranking the Cows(Bitset加速floyd求闭包传递)
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ woul ...
- redis:php-redis中有序集合 zset的使用
ZSET(stored set) 和 set 一样是字符串的集合,不同的是每个元素都会关联一个 double 类型的 score .实现使用的是 skip list 和 hash table , sk ...
- fiddler之使用教程(一)
一. 什么是fiddler&它可以做什么 fiddler是位于客户端和服务器端的HTTP代理,也是目前最常用的http抓包工具之一.它能够记录客户端和服务器之间的所有HTTP请求,可以针对特定 ...
- neutron ovs+vxlan
title: Neutron ovs+vxlan date: 2017-04-26 23:37 tags: Network 主机网卡配置 controller: ens160:192.168.11.1 ...
- Cassandra二级索引原理——新创建了一张表格,同时将原始表格之中的索引字段作为新索引表的Primary Key,并且存储的值为原始数据的Primary Key,然后再通过pk一级索引找到真正的值
1.什么是二级索引? 我们前面已经介绍过Cassandra之中有各种Key,比如Primary Key, Cluster Key 等等.如果您对这部分概念并不熟悉,可以参考之前的文章: [Cassan ...
- day5-xml模块
一.简述 xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.它用于不同语言或者程序之间进行数据交换,从这点上讲与json差不多,只不过json看 ...
- Eclipse 3.7 极述优化
1.去除不用的jar Eclipse/plugins目录下去除下面的jar和目录 2012/02/17 03:09 14,169 org.eclipse.cvs_1.1.100.v2012020808 ...