Jquery's ajax () function implements a solution for garbled Chinese characters, jqueryajax

Source: Internet
Author: User

Jquery's ajax () function implements a solution for garbled Chinese characters, jqueryajax

$. Ajax ({

DataType: 'json', type: 'post', url: 'http: // localhost/test. do ', data: {id: 1, type: 'item'}, success: function (data ){}});
Problem:
When the background action program is submitted, the type obtained is garbled
Solution:
Method 1: encodeURI is used for encoding before submission. Remember that it must be twice.
1. Modify the following code:
Data: {id: 1, type: encodeURI ('item '))}

2. decode the obtained string in the background action.


1. String type = request. getParameter ("type ");

2. type = URLDecoder. decode (type, "UTF-8 ″);

Method 2: ajax configure the contentType attribute, add charset = UTF-8
Add the following parameters to the ajax method:
ContentType: "application/x-www-form-urlencoded; charset = UTF-8" use other js frameworks or xhr are similar, set the contentType in the header,
The key here is charset = UTF-8, if there is no such, it will not work, the default contentType in jQuery is not

Problem:

Garbled characters appear when jquery ajax finishes processing data in the background and passes it to the front-end jsp page.

Solution: add this sentence to the background Java code, response. setCharacterEncoding ("UTF-8 ");

Related Article

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.