我在连接到webservice时遇到问题,如果通过url get参数调用,则返回JSON。 这个调用程序是用AJAX实现的,这里的问题是"调用" Applikation是一个基于web的phonegap应用程序,它通过AJAX从Server调用数据。 为了允许跨域起源ajax调用,我已经在我的php服务器端代码中为它设置了标题:
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET");
header("Access-Control-Allow-Credentials: true");
echo json_encode($array);
问题是我尝试auth之前,auth工作,如果我直接在浏览器中调用url,并且所有json调用都可能在...之后...只有当我使用ajax时,zend_auth身份会丢失,它总会返回我没有登录 这是我的AJAX JS代码:
function loadTimeline()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
addItems(xmlhttp.responseText);
}
}
xmlhttp.open("GET","http://wishlistserver.localhost/timeline/get",true);
xmlhttp.send();
}
function auth(username,password)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
}
}
xmlhttp.open("GET","http://wishlistserver.localhost/Login/login?username=" + username + "&password=" + password,true);
xmlhttp.send();
}
这是检查我是否已登录的功能:
if (!Zend_Auth::getInstance()->hasIdentity()) {
$errorInformation = array();
$errorInformation["message"] = "No session";
$errorInformation["code"] = "3";
Application_Model_JsonajaxHandle::sendJSON($errorInformation);
//end execution of controller actions
exit;
}
我期待着你的帮助。非常感谢你!
答案 0 :(得分:0)
这可能是解释:会话ID(在您登录后由服务器使用)作为cookie发送,并且由于请求是跨域的,因此浏览器将其视为第三方cookie。有几个浏览器会阻止第三方cookie,会话丢失。
我遇到了类似的问题,并最终得到了这个解决方案:在客户端(在浏览器中)生成会话ID,使用Javascript sessionStorage存储会话ID,然后将每个请求的会话ID发送到服务器。
由于我没有找到很多好的答案,我写了两篇详细说明解决方案的文章: