假设我在first.com中说一个用户设置了一个cookie。现在,我想通过javascript和ajax在second.com中读取该cookie。但这不起作用。我有xmlHttp.status
= 0。
样例代码
在第二个域的readcookie.php文件中
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}
function readcookie(){
createXMLHttpRequest();
xmlHttp.open("GET","http://www.first.com/cookie.php",true);
xmlHttp.onreadystatechange=getcookie;
xmlHttp.send(null);
}
function getcookie(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var reply=xmlHttp.responseText;
if(reply){
alert(reply);
}
}
else
alert(xmlHttp.status);
}
}
在第一个域cookie.php文件中
if(isset($_COOKIE['user'])){
echo $_COOKIE['user'];
}
else{
setcookie('user','a2345',0);
echo $_COOKIE['user'];
}