Ajax Chinese garbled various solutions summary _ajax related

Source: Internet
Author: User
Because the XMLHTTP uses the Unicode code uploads the data, but the General page uses is the gb2312, this causes the display page to produce the garbled code. And when the page to get the XMLHTTP return is the UTF-8 encoding, which caused the display generated garbled.

One solution is to use encodeURIComponent Plus modified content-type for application/x-www-form-urlencoded "to encode the data in a URL format, and, of course, to specify the encoding, such as:" application/x-www-form-urlencoded; Charset=utf-8 ",

one way to solve this problem

Example:
Copy Code code as follows:

Http_request = new ActiveXObject ("Msxml2.xmlhttp");
Http_request.setrequestheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8");


the solution for the second
is to display the declaration as GB2312 in the PHP file
Copy Code code as follows:

Header ("content-type:text/html;charset=gb2312");

And for the Chinese to send to the server transcoding.

As follows
Copy Code code as follows:

<?php
Header ("content-type:text/html;charset=gb2312");
if ($_post[' content ')]
{
$_post["Content"]=iconv ("UTF-8", "gb2312", $_post["content"]);
Print ("Contents is". $_post[' content ')];
}
Else
{
Print ("No content to send");
}
?>

Therefore, this can solve the garbled problem

Ajax Chinese garbled in JSP

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:
Use the provided escape () or encodeURI () method of JavaScript
For example:
Client:
Copy Code code as follows:

var url= "a.jsp?name= Xiao Li";
Url=encodeuri (URL);
Url=encodeuri (URL); Two times, very key [specifically why, I don't know]
/********************************************/

Also some people write Var url= "A.jsp?name=escape (" Xiao Li ")";
Features are similar to the encodeURI method.
Copy Code code as follows:

/********************************************/
Xmlhttp.setrequestheader ("Cache-control", "No-cache");
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.setrequestheader ("ContentType", "text/html;charset=uft-8")//Specify the encoding format for sending data
Xmlhttp.open ("Post", url,true);

Server side:
Copy Code code as follows:

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 format of UTF-8 decoding, 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:
In the JSP file:
Response.setcontenttype ("text/text;charset=utf-8")/txt text file returned
Or
Response.setcontenttype ("Text/xml;charset=utf-8")//returned XML file
gb2312 jquery ajax get Data gb2312 Yes jsp:<% @page contenttype= "Application/json" pageencoding= "gb2312"%> the JSON used here for testing, as text/html should
APs: Response.charset= "gb2312" (reference network not tested)
Php:header ("content-type:text/html; charset=gb2312 ");(reference network not tested)
HTML: unresolved
gb2312 jquery ajax get data utf-8 no  
utf-8 jquery ajax get Data utf-8 No  
utf-8 jquery ajax get Data gb2312 Yes jsp:<% @page contenttype= "Application/json" pageencoding= "gb2312"%> the JSON used here for testing, and text/ HTML should be the same as
aps:response.charset= "gb2312" (reference network not tested)
Php:header ("content-type:text/html; charset=gb2312 ");(reference network not tested)
HTML: unresolved
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.