$.ajax ({ DataType: ' JSON ', type: ' POST ', url: ' http://localhost/test/test.do ', data: {id:1, type: ' Product '},success:function (data) { } } ); Problem: When the back-end action program is submitted, the type being fetched is garbled Workaround: Method One: Use encodeURI two times before submitting the code, remember must be two times 1. Modify the following code
2. In the background action to decode the obtained string
1, String type = Request.getparameter ("type");
2, type = Urldecoder.decode (type, "Utf-8″");
Method Two: Ajax configuration contenttype properties, plus Charset=utf-8
Add the following parameters to the Ajax method
ContentType: "application/x-www-form-urlencoded; Charset=utf-8″ use other JS frame or XHR is similar, set header in ContentType can,
The key here is charset=utf-8, if not this, is not possible, the default jquery in the ContentType is not
First, test environment
jquery:1.3.2
tomcat:5.5.17
Second, test methods
1. Using Get method
Server-side Java code:
String name = new String (request.getparameter ("name"). GetBytes ("Iso8859-1"), "Utf-8");
Client JS Code:
$.ajax {url: ' 2.jsp ', type: ' Get ', data: {name: ' Chinese '},success:function (response) {
alert (response);
}});
jquery adds x-requested-with to the header when using AJAX, with the value: Xmlhttprequest,filter the character encoding to UTF8 when the AJAX request in jquery is judged to be This solves the Chinese garbled problem in post submission, and does not need to set request.setcharacterencoding ("UTF-8") in the Code;
For the Chinese garbled problem of get way, we suggest not to use get method to submit Chinese, all change to post ^-^
To be consistent with the way Prototype.js handles Chinese, you can customize the properties in the header by using the following methods RequestType
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.