Friends who have used Ajax must know that JavaScript uses UTF-8 international coding, that is, each Chinese character is stored in three bytes, but this leads to garbled code when sending data with Ajax.
One solution is to use encodeuricomponent and modify “content type” to “application / x-www-form-urlencoded” to uniformly encode the data into “URL” format. However, there is a disadvantage in doing so. Using PHP’s URLDecode can’t get the correct text at all
Another method is to convert the data into GB2312 format through the function written in VBScript. Personally, I think this method is better. Interested friends can check it online.
Today, I had a whim. Will cookies be sent when Ajax calls? I immediately wrote a program to test it. Sure enough, I can write the data into the cookie through JavaScript before calling Ajax, and then send the data in the cookie. It’s great!!
Demo address: http://cn5.cn/ajax/ajax12.htm
Client code: Ajax htm
<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, loading data failed. Reason:” + 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 $(a)
{
return document.getElementById(a);
}
</script>
<body>
Example of Ajax using cookies to transfer values: < br >
<form name=myform>
Name: < input id = name value = “variable name can even be 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 = “display returned results” > < br >
< div # id = m # bgcolor = blue > display status here < / div >
< input # type = button onclick = “alert (document. Cookie)” value = show local cookie >
</form>
Server side code: a.php
header(“Content-type: text/html;charset=GB2312”);
Echo “here are all the cookie variables you sent and their values \ n”;
print_r($_COOKIE);
?>