How to solve AJAX Chinese garbled characters

Source: Internet
Author: User
Tags send cookies

Friends who have used AJAX certainly know that javascript is using UTF-8 International encoding, that is, each Chinese character with 3 bytes to store, but this caused the use of AJAX to send data when garbled.
One solution is to use encodeURIComponent and modify Content-Type to application/x-www-form-urlencoded to uniformly encode data into url format. However, this solution has a disadvantage, using php urldecode cannot obtain the correct text at all.
Another method is to convert the data into gb2312 format using functions written in vbscript. I personally think this method is better. If you are interested, you can go to the Internet to check it.
Today, I was wondering, Will AJAX send cookies when calling them? I immediately wrote a program to test it. That's OK. Before calling ajax, I can write the data to the cookie through javascript, then you can send the data in the cookie. It's so nice !!

Client code ajax.htm

Copy codeThe Code is as follows:
<Title> example of using cookies to pass values through AJAX </title>
<Script>
Var oDiv
Var xh
Function getXML ()
{
Setcookie ($ ('name'). value, $ ('val'). value );
ODiv = document. all. m
ODiv. innerHTML = "loading... please wait ......."
ODiv. style. display = ""
Xh = new ActiveXObject ("Microsoft. XMLHTTP ")
Xh. onreadystatechange = getReady
Xh. open ("POST", "a. php", false)
Xh. send ();
}

Function getReady ()
{
If (xh. readyState = 4)
{
If (xh. status = 200)
{
ODiv. innerHTML = "done"
}
Else
{
ODiv. innerHTML = "sorry, failed to load data. Cause: "+ xh. statusText
}
}
} // Author: longbill www.longbill.cn

Function setcookie (name, value)
{
Var cookiestr = name + "=" + value + ";";
Var expires = "";
Var cookieexp = 60*60*1000;
Var d = new Date ();
D. setTime (d. getTime () + cookieexp );
Expires = "expires =" + d. toGMTString () + ";";
Document. cookie = cookiestr + expires;
}
Function $ ()
{
Return document. getElementById ();
}
</Script>

<Body>
Example of passing values using cookies in AJAX: <br>
<Form name = myform>
Name: <input id = name value = "variable name or even Chinese" size = 20> <br>
Value: <input type = text size = 20 id = val value = here> <br>
<Input onclick = "getXML ()" type = "button" value = "send data">
<Input onclick = "if (xh & xh. responseText) {alert (xh. responseText);}" type = "button" value = "show returned results"> <br>
<Div id = m bgcolor = blue> display status here </div>
<Input type = button onclick = "alert (document. cookie)" value = display local cookies>
</Form>

Server code a. php

Copy codeThe Code is as follows:
<?
Header ("Content-type: text/html; charset = GB2312 ");
Echo "The following are all COOKIE variables and their values \ n ";
Print_r ($ _ COOKIE );
?>

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.