Discussion and solution of the problem of Ajax Chinese garbled _ajax related

Source: Internet
Author: User
Ajax Chinese problems fall into two main categories:


1 The parameter in the Send path has Chinese, the parameter value in the server segment is garbled





For example:


var url= "a.jsp?name= Xiao Li";


Xmlhttp.open ("Post", url,true);





Solution:


uses JavaScript's provided escape () or encodeURI () method





For example:


Client:


var url= "a.jsp?name= Xiao Li";


Url=encodeuri (URL);


Url=encodeuri (URL); Two times, very key [specifically why, I don't know]


/********************************************/


also someone wrote Var url= "A.jsp?name=escape (" Xiao Li ")";

The
function is similar to the encodeURI method.


/********************************************/


Xmlhttp.setrequestheader ("Cache-control", "No-cache");


Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");


Xmlhttp.setrequestheader ("ContentType", "text/html;charset=uft-8")//Specifies the encoding format for sending data


Xmlhttp.open ("Post", url,true);





Server side:


String name = Request.getparameter ("name");


name = Java.net.URLDecoder.decode (name, "UTF-8");





2) The value of the returned ResponseText or Responsexml contained in Chinese is garbled


Reason: Ajax in the reception of ResponseText or Responsexml value is in accordance with the UTF-8 format to decode, if the server segment sent data is not UTF-8 format, Then the value of receiving responsetext or responsexml may be garbled.


Workaround: Specify the format for sending data on the server:





on the service side:


Copy Code code as follows:

Response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("text/text;charset=utf-8")/txt text file returned
Response.setcontenttype ("Text/xml;charset=utf-8")//returned XML file
Response.setcontenttype ("Application/json")//Return JSON file
PrintWriter bw = Response.getwriter ()//output to use this class
Bw.write (result);
Bw.flush ();
Bw.close ();



Summary: 1 The format of the AJAX submission data defaults to Utf-8, using JavaScript's provided escape () or encodeURI () method. Use Java.net.URLDecoder.decode when receiving on server ("", " UTF-8 ") method for decoding.


2) Xtmlhttp returns the data the default character encoding is Utf-8, so the server to send data to the client, also to use the Utf-8 encoding


if the above method still can not solve the garbled problem, then you try to save the Jsp,htm,java file in UTF-8 encoding format.


in short: the former background data interaction using UTF-8 encoding on the line.
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.