How to solve the problem of passing Chinese garbled characters in jquery. ajax URLs
This article mainly introduces how to solve the problem of passing Chinese garbled characters in the url of jquery. ajax. If you need it, you can refer to it for help.
JQuery
Default contentType of JQuery: application/x-www-form-urlencoded
This is the reason why JQuery is garbled, when the character set is not specified, is to use ISO-8859-1
ISO8859-1, usually called Latin-1. Latin-1 includes additional characters that are indispensable for writing all Western European languages.
JQuery's Ajax does not take into account the internationalization problem at all. It uses the European character set, so it causes the problem of garbled characters when passing Chinese characters.
Our UTF-8 can solve this problem.
Ultimately, JQuery code needs to be modified to explicitly declare that contentType uses the UTF-8 character set to solve the problem of GB2312 Chinese transmission.
1. Modify JQuery code
Just need simple JQuery code to modify, plus charset = UTF-8 can be, so do not need to change what web. you do not need to use escapc (str) to decode config or something on the page. How to deliver English and Chinese.
Modify the jquery file used: jquery-1.4.4.min.js
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 code:
The Code is as follows:
Function confirmcommit (){
Var wlCompany = $ ("# wlCompany"). val (); // It contains Chinese Characters
Var wlId = $ ("# wlId"). val ();
Var proposer = $ ("# proposer"). val ();
If (confirm ("are you sure you want to exchange ")){
$. Ajax ({
Type: 'post ',
Url: '$ {pageContext. request. contextPath}/returnGoods/confrimexico changegoods. do ',
Data: 'wlcompany = '+ wlCompany +' & wlId = '+ wlId +' & proposer = '+ proposer, // directly pass the value
DataType: 'text ',
Error: function (){
Alert ("JQuery AJAX Error! ");
},
Success: function (msg ){
Alert (msg );
Return;
If (msg = 'exchange successful '){
Document. location = "$ {pageContext. request. contextPath}/orderItem/queryProduceItem. do? OrderBusType = "+ $ {orderBusType };
}
}
});
}
}
3. Java code:
The Code is as follows:
Public ActionForward confrimchangegoods (ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Log.info ("confirm the replacement of confrimExchangeGoods start ...............");
Response. setCharacterEncoding ("UTF-8"); // set it here
String wlCompany = request. getParameter ("wlCompany ");
String wlId = request. getParameter ("wlId ");
String proposer = request. getParameter ("proposer ");
.....
}