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()函数传值中文乱码解决方法介绍的更多相关文章

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

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...

  2. ajax url参数中文乱码解决方法

    较好的处理办法,对js的url中的中文参数值使用两次encodeURI(),即encodeURI(encodeURI("url的中文参数值")) JS代码: var name=&q ...

  3. php mysql 中文乱码解决方法

    本文章向码农们介绍php mysql 中文乱码解决方法,对码农们非常实用,需要的码农可以参考一下. 从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行 解 ...

  4. Django 分页查询并返回jsons数据,中文乱码解决方法

    Django 分页查询并返回jsons数据,中文乱码解决方法 一.引子 Django 分页查询并返回 json ,需要将返回的 queryset 序列化, demo 如下: # coding=UTF- ...

  5. Java 前台后台数据传递、中文乱码解决方法

    1.向前台传递数据;2.向后台传递数据;3.ajax post 提交数据到服务端时中文乱码解决方法;4.数组类型参数传递; 1.向前台传递数据:1.1 字符串数据传递:  这种方式只是单一的向前台传递 ...

  6. [转]mysql导入导出数据中文乱码解决方法小结

    本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...

  7. Zxing中文乱码解决方法

    Zxing中文乱码解决方法总结 尝试过非常多方法  最后发现此方法解决的乱码最多....... 在百度搜索二维码图片 经过前2页的測试  除开一张图之外  其余都能扫描出结果 假设大家有更好的解决方法 ...

  8. unity3d 中文乱码解决方法——cs代码文件格式批量转化UTF8

    在Unity3d中经常会碰到中文乱码的问题,比如代码中的[AddComponentMenu("GameDef/AI/战机AI")],注释,中文文本等等 其原因在于,unity本身是 ...

  9. Codeblocks中文乱码解决方法

    odeblocks中文乱码解决方法: 特别提示:出现中文乱码情况才执行以下操作,未出现请勿随意修改!!!! 打开Codeblocks -> 设置 -> 编辑器: 然后点击 Encoding ...

随机推荐

  1. StrStr,判断一个字符串是不是另一个字符串的字串,并返回子串的位置

    public int strStr(String haystack, String needle) { if(haystack == null || needle == null) { return ...

  2. mysql desc esc 基本命令总结

    asc 按升序排列desc 按降序排列 下列语句部分是Mssql语句,不可以在access中使用. SQL分类:DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)DML—数据操 ...

  3. Linux常用命令.rpm

    1.安装: rpm -ivh 包全名(查询依赖网址:http://www.rpmfind.net) -i(install):安装 -v(verbose):显示详细信息 -h(hash):显示进度 -- ...

  4. SSH登陆阿里云服务器出现Permission denied (publickey)错误解决方案

    操作环境: 操作系统:Mac10.11.5 阿里云服务器:Ubuntu16.04 远程连接:SSH 注:首先我们已假设你已经自己生成了SSH秘钥,并已经配置到阿里云.绑定了自己的云服务器. 但是后来发 ...

  5. ConEmu

    https://conemu.github.io/ https://github.com/Maximus5/ConEmu/releases 将控制台整合到一起的工具,支持cmd.powershell. ...

  6. 完全卸载gitlab

    完全卸载删除gitlab 2017年5月29日 wuhao 暂无评论 4,089次浏览   完全卸载删除gitlab 1.停止gitlab   1 gitlab-ctl stop 2.卸载gitlab ...

  7. Prism开发人员指南5-WPF开发 文档翻译(纯汉语版)

    2014四月       Prism以示例和文档的形式帮助你更简单的设计丰富灵活易维护的WPF程序.其中使用的设计模式体现了一些重要的设计原则,例如分离关注点和松耦合,Prism帮助你利用松耦合组件设 ...

  8. Struts01---入门小案例

    创建web项目    实现的效果! 用户点击页面不同的链接,后台调用不同的代码! 创建两个类实现共同的接口! public interface Action { String execute(); } ...

  9. LeetCode OJ:Search for a Range(区间查找)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  10. thinking java

    public class CrossContainerIteration{ public static void display(Iterator<Pet> it){ while(it.h ...