jquery Ajax return value garbled solution

Source: Internet
Author: User
Tags constant tomcat server

Instance resolution method

encodeURI (encodeURI (data))///Note two times encoded!!
$.ajax ({
Type: "Post",
Url:url,
Data:data,
ContentType: "Application/x-www-form-urlencoded;charset=utf-8",
Cache:false,
Success:viewajax.handle (this.pdomobj, This.div, URL),
Error:viewajax.error (this.pdomobj, This.div, URL)
});


Java:

string queryvalue = Java.net.urldecoder.decode ((string) request.getparameter (Querycode), "Utf-8");


Method Two

Ajax to send the request the default encoding is UTF-8, front and server coding to be unified this is not allowed to repeat, but even so, accidents still occurred;;;;;
Web effects://Note the following two times transcoding, on the server side is also carried out two times transcoding to get the normal data

$ ("#querybyname"). Click (function () {
var name = encodeURI (encodeURI ($ ("#qname"). Val ());
$.ajax ({
Type: "Post",
URL: "Querybyname.action",
ContentType: "Application/x-www-form-urlencoded;charset=utf-8",
data:{
Queryname:name
},
Success:function (msg) {
$ ("#signwork"). HTML (msg);
}
})
})

Server:struts2

string name =getqueryname ();
Name =java.net.urldecoder.decode (name, "Utf-8");
System.out.println ("action......bbbb" +name);
String Name01=java.net.urldecoder.decode (Name, "Utf-8");
System.out.println ("... cccc") ("+name01");

Here two times the transfer code to obtain the correct data, the specific reasons for the analysis;
It is said that there is another simple way to set up
Tomcat Server.xml configuration file
Uriencoding's OK.


Finally found the source of the problem, in fact, my struts2 configuration file has a little problem

This configuration information is available in the configuration file

<constant name= "struts.i18n.encoding" value= "Utf-8" ></constant>

This must be consistent with the page code, if the use of Ajax to send the request of the default Utf-8 encoding, but the background struts configuration file is GBK, let me very depressed!! Finally, the use of regular action submitted when the garbled, only to find that there is a problem. In fact, if all the use of UTF-8 encoding will not appear garbled

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.