欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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);
}

相關(guān)文章

  • jQuery之過濾元素操作小結(jié)

    jQuery之過濾元素操作小結(jié)

    這篇文章主要介紹了jQuery中的過濾元素操作。需要的朋友可以過來參考下,希望對大家有所幫助
    2013-11-11
  • jquery實現(xiàn)可自動收縮的TAB網(wǎng)頁選項卡代碼

    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用法實例分析(附demo示例源碼)

    這篇文章主要介紹了jQuery插件之jQuery.Form.js用法,結(jié)合實例形式分析了jQuery.Form.js的具體使用技巧與相關(guān)注意事項,需要的朋友可以參考下
    2016-01-01
  • JQuery日歷插件My97DatePicker日期范圍限制

    JQuery日歷插件My97DatePicker日期范圍限制

    這篇文章主要介紹了JQuery日歷插件My97DatePicker日期范圍限制的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 最新評論