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 ...
随机推荐
- Luci - UCI (Unified Configuration Interface)
参考: http://wiki.openwrt.org/doc/techref/uc http://luci.subsignal.org/api/luci/modules/luci.model.uci ...
- The Building Blocks-Enterprise Applications Part 2- Information Management and Business Analytics
1. Business Analytic Applications Data Analytics Also referred to as 'Business Analytics' or 'Busine ...
- 采用Bash脚本性能监控过程
为一个Linux过程监控,采用Bash脚本. 采用ps命令的过程监控,使用周期加上连续监测的睡眠时间. 使用方法: psmonitor.sh -p [pid] -d [interval] -n [st ...
- poj3233(矩阵快速幂)
poj3233 http://poj.org/problem?id=3233 给定n ,k,m 然后是n*n行, 我们先可以把式子转化为递推的,然后就可以用矩阵来加速计算了. 矩阵是加速递推计算的一 ...
- 从零開始学android<数据存储(1)SharedPreferences属性文件.三十五.>
在android中有五种保存数据的方法.各自是: Shared Preferences Store private primitive data in key-value pairs. 相应属性的键值 ...
- 工作经常使用的SQL整理,实战篇(二)
原文:工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...
- ROW_NUMBER() OVER 排序函数的基本用法
ROW_NUMBER() OVER 排序函数 select ROW_NUMBER() OVER (PARTITION BY guide_id ORDER BY pic_sort) as rowid,p ...
- Chromium Graphics: GPUclient的原理和实现分析之间的同步机制-Part II
摘要:Part I探析GPUclient之间的同步问题,以及Chromium的GL扩展同步点机制的基本原理.本文将源码的角度剖析同步点(SyncPoint)机制的实现方式. 同步点机制的实现主要涉及到 ...
- 【原创】leetCodeOj --- Find Peak Element 解题报告
题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...
- Greenplum+Hadoop学习笔记-14-定义数据库对象之创建与管理模式
6.3.创建与管理模式 概述:DB内组织对象的一种逻辑结构.一个DB内能够有多个模式.在未指定模式时默认放置在public中.能够通过"\dn"方式查看数据库中现有模式: test ...