Solution to the problem of passing Chinese garble in the url of jquery. Ajax

  • 2020-03-30 01:39:12
  • OfStack

JQuery

JQuery default contentType: application/x - WWW - form - urlencoded

This is why JQuery is scrambling, using iso-8859-1 when no character set is specified

Iso8859-1, commonly called latin-1. Latin-1 includes additional characters essential for writing all western European languages.

JQuery's Ajax does not take internationalization into account at all and USES the European character set, which causes the problem of sending garbled Chinese characters.

Our utf-8 solves this problem.

Finally refers to the code that needs to modify JQuery to explicitly declare that the contentType USES the utf-8 character set to solve the GB2312 Chinese delivery problem.

1. Modify the JQuery code

Simply change the JQuery code and add charset= utf-8, so you don't need to change anything web.config or change anything in the page, and you don't need to use escapc(STR) to decode on the server side. As English is delivered, so is Chinese.

Modify the jquery file used :jquery-1.4.4.min.js

AjaxSettings: {url: the location. The href, global: true, type: "GET", contentType: "application/x - WWW - form - urlencoded ; Charset = utf-8 "ProcessData: true, async: true, XHR: function () {return new E.X MLHttpRequest}

2. Js code:


function confirmcommit(){
    var wlCompany = $("#wlCompany").val();//This contains Chinese 
    var wlId = $("#wlId").val();
    var proposer = $("#proposer").val();
    if(confirm(" Are you sure you want a replacement ")){
$.ajax({
type:'POST',
url:'${pageContext.request.contextPath}/returnGoods/confrimExchangeGoods.do',
data:'wlCompany='+wlCompany+'&wlId='+wlId+'&proposer='+proposer, //Direct pass value 
dataType:'text',
error:function(){
    alert("JQuery AJAX Error!");      
},
success:function(msg){
    alert(msg);
    return;
    if(msg==' Exchange success '){
 document.location="${pageContext.request.contextPath}/orderItem/queryProduceItem.do?orderBusType="+${orderBusType};
    }
}
});
     }
 }

3.Java code:

public ActionForward confrimExchangeGoods(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
log.info(" Confirm a replacement  confrimExchangeGoods start...............");
response.setCharacterEncoding("UTF-8"); //I'm going to set 
String wlCompany = request.getParameter("wlCompany");
String wlId = request.getParameter("wlId");
String proposer = request.getParameter("proposer");
     .....
}


Related articles: