jquery的ajax()函數(shù)傳值中文亂碼解決方法介紹
$.ajax({
dataType : ‘json',type : ‘POST',url : ‘http://localhost/test/test.do',data : {id: 1, type: ‘商品'},success : function(data){ } } );
問(wèn)題:
提交后后臺(tái)action程序時(shí),取到的type是亂碼
解決方法:
方法一:提交前采用encodeURI兩次編碼,記住一定是兩次
1.修改以下代碼
data:{id:1, type:encodeURI(encodeURI(‘商品'))}
2.在后臺(tái)action里要對(duì)取得的字符串進(jìn)行decode
1、String type = request.getParameter(“type”);
2、type = URLDecoder.decode(type, “UTF-8″);
方法二:ajax配置contentType屬性,加上charset=UTF-8
在ajax方法中加入以下參數(shù)
contentType: “application/x-www-form-urlencoded; charset=UTF-8″使用其它js框架或者xhr都是差不多,設(shè)置header中contentType即可,
這里關(guān)鍵是charset=UTF-8,如果沒(méi)有這個(gè),是不行的,默認(rèn)jQuery里的contentType是沒(méi)有的
一、測(cè)試環(huán)境
jQuery:1.3.2
tomcat:5.5.17
二、測(cè)試方法
1.使用get方式
服務(wù)器端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);
}});
結(jié)果:正確顯示
$.ajax({url: "2.jsp",type: "get",data: "name=中文",success: function(response){
alert(response);
}});
結(jié)果:亂碼
$.get("2.jsp", { name: "中文" },function(response){
alert(response);
});
結(jié)果:正確顯示
$.get("2.jsp", "name=中文",function(response){
alert(response);
});
結(jié)果:亂碼
2.post方式
服務(wù)器端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);
}});
結(jié)果:正確顯示
$.ajax({url: "3.jsp",type: "post",data: {name:"中文"},success: function(response){
alert(response);
}});
結(jié)果:正確顯示
$.post("3.jsp", { name: "中文" },function(response){
alert(response);
});
結(jié)果:正確顯示
$.post("3.jsp", "name=中文",function(response){
alert(response);
});
結(jié)果:正確顯示
三、使用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的時(shí)候會(huì)在header中加入X-Requested-With,值為:XMLHttpRequest,filter中判斷是jQuery的ajax請(qǐng)求時(shí)就把字符編碼設(shè)為utf8,這樣可以解決post提交中的中文亂碼問(wèn)題,不需要在代碼中設(shè)置request.setCharacterEncoding("UTF-8");
對(duì)于get方式的中文亂碼問(wèn)題,建議不使用get方式提交中文,統(tǒng)統(tǒng)改為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("錯(cuò)誤:" + 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使用ajaxSubmit()提交表單示例
- jquery實(shí)現(xiàn)ajax提交form表單的方法總結(jié)
- jquery中ajax處理跨域的三大方式
- jquery ajax提交表單數(shù)據(jù)的兩種方式
- jquery ajax例子返回值詳解
- JQuery AJAX提交中文亂碼的解決方案
- jquery ajax 向后臺(tái)傳遞數(shù)組參數(shù)示例
- 基于JQuery框架的AJAX實(shí)例代碼
- jquery的ajax異步請(qǐng)求接收返回json數(shù)據(jù)實(shí)例
- jQuery Ajax異步處理Json數(shù)據(jù)詳解
- jQuery實(shí)現(xiàn)Ajax功能分析【與Flask后臺(tái)交互】
相關(guān)文章
jQuery中offsetParent()方法用法實(shí)例
這篇文章主要介紹了jQuery中offsetParent()方法用法,實(shí)例分析了offsetParent()方法的功能、定義及返回匹配元素所有祖先元素中第一個(gè)采用定位的祖先元素時(shí)的使用技巧,需要的朋友可以參考下2015-01-01
jquery ajax實(shí)現(xiàn)下拉框三級(jí)無(wú)刷新聯(lián)動(dòng),且保存保持選中值狀態(tài)
jquery ajax實(shí)現(xiàn)下拉框三級(jí)無(wú)刷新聯(lián)動(dòng),且保存保持選中值狀態(tài)。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10
jQuery ui autocomplete選擇列表被Bootstrap模態(tài)窗遮擋的完美解決方法
這篇文章主要介紹了jQuery ui autocomplete選擇列表被Bootstrap模態(tài)窗遮擋的完美解決方法,本文介紹的非常詳細(xì),解決過(guò)程思路明了,需要的朋友可以參考下2016-09-09
jQuery獲取所有父級(jí)元素及同級(jí)元素及子元素的方法(推薦)
這篇文章主要介紹了jQuery獲取所有父級(jí)元素及同級(jí)元素及子元素的方法,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值 ,需要的朋友可以參考下2018-01-01
jQuery打字效果實(shí)現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了jQuery打字效果實(shí)現(xiàn)方法,詳細(xì)分析了jQuery實(shí)現(xiàn)打字效果所涉及的jticker_split.js插件機(jī)具體調(diào)用技巧,并附帶完整的demo源碼供讀者下載參考,需要的朋友可以參考下2015-12-12
jQuery實(shí)現(xiàn)左右兩個(gè)列表框的內(nèi)容相互移動(dòng)功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)左右兩個(gè)列表框的內(nèi)容相互移動(dòng)功能,涉及jQuery事件響應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01
基于jQuery實(shí)現(xiàn)網(wǎng)頁(yè)打印功能
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)網(wǎng)頁(yè)打印功能,實(shí)現(xiàn)的打印功能大致跟瀏覽器的Ctrl+P效果一樣,感興趣的小伙伴們可以參考一下2015-12-12
詳解JavaScript中jQuery和Ajax以及JSONP的聯(lián)合使用
這篇文章主要介紹了詳解JavaScript中jQuery和Ajax以及JSONP的聯(lián)合使用,jQuery庫(kù)和Ajax異步結(jié)構(gòu)以及JSON數(shù)據(jù)傳輸也是JS日常編程中最常用到的東西,需要的朋友可以參考下2015-08-08

