当我在JQuery中使用getJSON到外部域时,发出的请求不包含该域的cookie.我正在将其用于正在编写的分析脚本,并且需要在脚本运行所在的外部域上设置Cookie,以便跟踪唯一身份访问者.
domain1.com/website.html
domain2.com/tracker.js
//Get information about the user
info = "(here's some things about the user)";
//Send data using JSON
$.getJSON("http://domain2.com/getdata.PHP?"+info,function(data){}
);
domain2.com/getdata.PHP
/******
* Code to save data and stuff
*******/
//Get the current cookie (if any).
$current_tid = $_COOKIE['tID'];
//checks if the cookie is a string of 50 characters
if (strlen($current_tid)==50){
$TrackerID = $current_tid; //If the cookie already have a unique string,then use it!
} else {
$TrackerID = random_gen(50); //Generates a new random string with 50 characters
}
//Set cookie "tID" with the unique variable $TrackerID
setcookie("tID",$TrackerID,time()+60*60*24*365);
因此,事实是,当用户在server1上加载website.html时,用户也在server2上加载了tracker.js,后者将带有JSON的数据发送到getdata.PHP.但是,该脚本不会发送cookie,并且每次加载脚本时getdata.PHP都会生成一个新字符串.
有什么方法可以使用JSON发送Cookie?