這次帶給大家jquery.ajax中url傳遞中文顯示亂碼如何處理,jquery.ajax中url傳遞中文顯示亂碼處理的注意事項有哪些,下面就是實戰案例,一起來看一下。
JQuery
JQuery預設的contentType:application/x-www-form-urlencoded
這才是JQuery正在亂碼的原因,在未指定字元集的時候,是使用ISO-8859-1
ISO8859-1,通常叫做Latin-1。 Latin-1包括了書寫所有西方歐洲語言中不可缺少的附加字元。
JQuery的Ajax完全沒有考慮到國際化的問題,使用了歐洲的字元集,所以才引起了傳遞中文出現亂碼的問題。
而我們的UTF-8則可以解決這個問題。
最終指需要修改JQuery的程式碼,明確聲明contentType使用utf-8字元集,即可解決GB2312中文傳遞的問題。
1. 修改JQuery程式碼
只需要簡單的將JQuery的程式碼加以修改,加上charset=UTF-8就可以了,這樣不需要改變改什麼web.config或什麼在頁面中改編碼什麼的了,也不需要用escapc(str)再在服務端解碼。英文怎麼傳遞,中文也怎麼傳遞。
修改用到的jquery檔:jquery-1.4.4.min.js
1 2 3 4 | ajaxSettings:
{url:location.href, global :true,type:"GET",contentType:"application/x-www-form-urlencoded
;charset=UTF-8
",processData:true,async:true,xhr: function (){ return new E.XMLHttpRequest}
|
登入後複製
2. Js程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function confirmcommit(){
var wlCompany = $("#wlCompany").val();
var wlId = $("#wlId").val();
var proposer = $("#proposer").val();
if (confirm("确认要换货吗")){
$.ajax({
type:'POST',
url:'${pageContext.request.contextPath}/returnGoods/confrimExchangeGoods. do ',
data:'wlCompany='+wlCompany+'&wlId='+wlId+'&proposer='+proposer,
dataType:'text',
error: function (){
alert("JQuery AJAX Error!");
},
success: function (msg){
alert(msg);
return ;
if (msg=='换货成功'){
document.location="${pageContext.request.contextPath}/orderItem/queryProduceItem. do ?orderBusType="+${orderBusType};
}
}
});
}
}
|
登入後複製
3 .Java程式碼:
1 2 3 4 5 6 7 8 | public ActionForward confrimExchangeGoods(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
log.info("确认换货 confrimExchangeGoods start...............");
response.setCharacterEncoding("UTF-8");
String wlCompany = request.getParameter("wlCompany");
String wlId = request.getParameter("wlId");
String proposer = request.getParameter("proposer"); .....}
|
登入後複製
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
iframe中頁面錨點失效如何處理
取得document物件步驟中document物件步驟詳解
################################################# #
以上是jquery.ajax中url傳遞中文顯示亂碼如何處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!