Home > Article > Web Front-end > Solution to the problem of Chinese garbled characters being passed in the URL of jquery.ajax_jquery
JQuery
JQuery default contentType: application/x-www-form-urlencoded
This is the reason why JQuery is garbled. When the character set is not specified, ISO-8859-1 is used
ISO8859-1, usually called Latin-1. Latin-1 includes additional characters indispensable for writing all Western European languages.
JQuery’s Ajax did not consider the issue of internationalization at all and used the European character set, which caused the problem of garbled characters when transmitting Chinese.
Our UTF-8 can solve this problem.
Ultimately, it means that you need to modify the JQuery code and explicitly declare that the contentType uses the utf-8 character set, which can solve the problem of GB2312 Chinese transmission.
1. Modify JQuery code
You only need to simply modify the JQuery code and add charset=UTF-8. In this way, there is no need to change web.config or change the encoding on the page, and there is no need to use escapc. (str) is then decoded on the server side. How it is conveyed in English is also conveyed in 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:
var wlCompany = $("#wlCompany").val();//This contains Chinese
var wlId = $("#wlId").val();
var proposer = $("#proposer").val();
if(confirm("Are you sure you want to exchange the product?")){
$.ajax({
type:'POST',
url:'${pageContext.request.contextPath}/returnGoods/confrimExchangeGoods.do',
data:'wlCompany=' wlCompany '&wlId=' wlId '&proposer=' proposer, //pass the value directly
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};
}
}
});
}
}
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
log.info("Confirm exchange confrimExchangeGoods start.............");
response.setCharacterEncoding("UTF-8"); //You need to set it here
String wlCompany = request.getParameter("wlCompany");
String wlId = request.getParameter("wlId");
String proposer = request.getParameter("proposer");
.....
}