jquery的ajax()函數(shù)傳值中文亂碼解決方法介紹
更新時間:2012年11月08日 14:36:21 作者:
jquery的ajax()函數(shù)傳值中文亂碼解決方法介紹,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
$.ajax({
dataType : ‘json',type : ‘POST',url : ‘http://localhost/test/test.do',data : {id: 1, type: ‘商品'},success : function(data){ } } );
問題:
提交后后臺action程序時,取到的type是亂碼
解決方法:
方法一:提交前采用encodeURI兩次編碼,記住一定是兩次
1.修改以下代碼
復(fù)制代碼 代碼如下:
data:{id:1, type:encodeURI(encodeURI(‘商品'))}
2.在后臺action里要對取得的字符串進(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,如果沒有這個,是不行的,默認(rèn)jQuery里的contentType是沒有的
一、測試環(huán)境
jQuery:1.3.2
tomcat:5.5.17
二、測試方法
1.使用get方式
服務(wù)器端java代碼:
復(fù)制代碼 代碼如下:
String name = new String(request.getParameter("name").getBytes("iso8859-1"),"utf-8");
客戶端js代碼:
復(fù)制代碼 代碼如下:
$.ajax({url: "2.jsp",type: "get",data: {name:"中文"},success: function(response){
alert(response);
}});
結(jié)果:正確顯示
復(fù)制代碼 代碼如下:
$.ajax({url: "2.jsp",type: "get",data: "name=中文",success: function(response){
alert(response);
}});
結(jié)果:亂碼
復(fù)制代碼 代碼如下:
$.get("2.jsp", { name: "中文" },function(response){
alert(response);
});
結(jié)果:正確顯示
復(fù)制代碼 代碼如下:
$.get("2.jsp", "name=中文",function(response){
alert(response);
});
結(jié)果:亂碼
2.post方式
服務(wù)器端java代碼:
復(fù)制代碼 代碼如下:
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
客戶端js代碼:
復(fù)制代碼 代碼如下:
$.ajax({url: "3.jsp",type: "post",data: "method=testAjaxPost&name=中文",success: function(response){
alert(response);
}});
結(jié)果:正確顯示
復(fù)制代碼 代碼如下:
$.ajax({url: "3.jsp",type: "post",data: {name:"中文"},success: function(response){
alert(response);
}});
結(jié)果:正確顯示
復(fù)制代碼 代碼如下:
$.post("3.jsp", { name: "中文" },function(response){
alert(response);
});
結(jié)果:正確顯示
復(fù)制代碼 代碼如下:
$.post("3.jsp", "name=中文",function(response){
alert(response);
});
結(jié)果:正確顯示
三、使用filter
復(fù)制代碼 代碼如下:
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請求時就把字符編碼設(shè)為utf8,這樣可以解決post提交中的中文亂碼問題,不需要在代碼中設(shè)置request.setCharacterEncoding("UTF-8");
對于get方式的中文亂碼問題,建議不使用get方式提交中文,統(tǒng)統(tǒng)改為post ^-^
為了和prototype.js處理中文的方式一致,可以使用如下的方式,自定義header中的屬性RequestType
復(fù)制代碼 代碼如下:
$.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代碼如下:
復(fù)制代碼 代碼如下:
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實現(xiàn)ajax提交form表單的方法總結(jié)
- jquery中ajax處理跨域的三大方式
- jquery ajax提交表單數(shù)據(jù)的兩種方式
- jquery ajax例子返回值詳解
- JQuery AJAX提交中文亂碼的解決方案
- jquery ajax 向后臺傳遞數(shù)組參數(shù)示例
- 基于JQuery框架的AJAX實例代碼
- jquery的ajax異步請求接收返回json數(shù)據(jù)實例
- jQuery Ajax異步處理Json數(shù)據(jù)詳解
- jQuery實現(xiàn)Ajax功能分析【與Flask后臺交互】
相關(guān)文章
jQuery中fadeIn、fadeOut、fadeTo的使用方法(圖片顯示與隱藏)
jQuery中fadeIn、fadeOut、fadeTo的使用方法(圖片顯示與隱藏),需要的朋友可以參考一下2013-05-05解析頁面加載與js函數(shù)的執(zhí)行 onload or ready
這篇文章主要介紹了頁面加載與js函數(shù)的執(zhí)行 onload or ready 需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12JQuery頁面隨滾動條動態(tài)加載效果的簡單實現(xiàn)(推薦)
下面小編就為大家?guī)硪黄狫Query頁面隨滾動條動態(tài)加載效果的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

jquery實現(xiàn)可自動收縮的TAB網(wǎng)頁選項卡代碼
這篇文章主要介紹了jquery實現(xiàn)可自動收縮的TAB網(wǎng)頁選項卡代碼,涉及jquery鼠標(biāo)事件及頁面元素樣式變換的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
2015-09-09 
jQuery插件之jQuery.Form.js用法實例分析(附demo示例源碼)
這篇文章主要介紹了jQuery插件之jQuery.Form.js用法,結(jié)合實例形式分析了jQuery.Form.js的具體使用技巧與相關(guān)注意事項,需要的朋友可以參考下
2016-01-01 
JQuery日歷插件My97DatePicker日期范圍限制
這篇文章主要介紹了JQuery日歷插件My97DatePicker日期范圍限制的相關(guān)資料,需要的朋友可以參考下
2016-01-01