我想知道是否有办法在Ajax响应中设置会话变量,基本上看起来像这样:
// this is response
function(response)
{
<?php $_SESSION['ID'] = response.id ?>
// or something similar to this?
}
然后我需要使用会话以便我可以访问它。
// (Some php file)
$var_id = $_SESSION['ID'];
这可行吗?
答案 0 :(得分:1)
以下是设置,获取和删除Cookie的三个功能
https://jsfiddle.net/stevenkaspar/gnoj9aop/
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,c.length);
}
}
return "";
}
function deleteCookie(cname){
document.cookie = cname+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
答案 1 :(得分:0)
您可以使用HTML5 sessionStorage对象在本地保存数据,但是对于单个会话。当用户关闭特定浏览器选项卡时,将删除数据。
sessionStorage.id = response.id ;