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 wrote Program Test it. You can write the data to the cookie through javascript before calling Ajax, and then send the data to the cookie, great !!
Demo address: http://cn5.cn/ajax/ajax12.htm
ClientCodeAjax.htmCopy codeThe Code is as follows: <title> example of passing values using cookies in 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. php copy Code the 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);
?>