How to solve the problem of passing Chinese garbled characters in jquery. ajax URLs

Source: Internet
Author: User

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

.....

}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.