Summary of Ajax Chinese garbled solution method

Source: Internet
Author: User

Ajax garbled Solution One:

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
Php:header ("content-type:text/html;charset=gb2312");

Ajax garbled Solution Two:
PHP send Chinese, Ajax receive
Just add a sentence to the top of PHP:
Header (' content-type:text/html;charset=gb2312 ');
XMLHTTP will correctly parse the Chinese in them.

Ajax send Chinese, PHP receive
This is more complicated:
In Ajax, first use encodeURIComponent to encode the Chinese to be submitted
In PHP:
$GB 2312string=iconv (' UTF-8 ', ' Gb2312//ignore ', $RequestAjaxString);
PHP writes MySQL database
When you set up conn, query the set names "gb2312"

==================================================

Note the red part!!! Otherwise you cannot use!!! Specific reason to guess yourself!
Iconv ("UTF-8", "GBK", $gametypes ["name"]);
Original:

My garbled problem is the AJAX data generated garbled, the original page is the use of GBK code. Later displayed as garbled, I use UE to convert it into UTF-8,
The original elements on the page can be displayed correctly. However, the value of the database is still garbled.
Ob_get_contents ();
Remove all the display data, and then output to the log file, the display is all garbled.
Using the UE to convert the file to GBK and then output, the display is correct.
Use here again: header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK ');
The foreground page appears as normal.


After trying this n many methods, many people did not find that the solution is actually very simple, and its answer in our previous processing of JSP garbled. Let's look at the classic Ajax request code first.

Xmlhttp.open ("Post", url, async);
Xmlhttp.setrequestheader ("Content-type", "text/html");
Xmlhttp.send (params);

Through the previous instructions, do not know you now see the clue is not. Do not know is affected by the online tutorials or other aspects of the impact, setRequestHeader and is a constant, and no one wants to change it, and the problem is just out of this place. Recall the encoding setting for the content of a JSP page, which has this section:

Contenttype= "text/html; Charset=utf-8 "
Now that we know the problem, we have to change the second code to read:
Xmlhttp.setrequestheader ("Content-type", "text/html;charset=utf-8");
Finally, don't forget to set it up when you return the data:

Response.setcontenttype ("Text/xml");
Response.setcharacterencoding ("UTF-8");

The several Ajax Chinese garbled solutions mentioned above are all using header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK '); This kind of method unifies the page's sending and accepting processing data page code, everybody can now try.

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.