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 !!
Demo address: http://cn5.cn/ajax/ajax12.htm
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 );
?>