admin管理员组文章数量:1296915
i have ajax function like below:
$.ajax({
url:"cookie.php",
type: 'post',
data: {'ok': val},
success:function(data) {
alert(data);
}
});
and my cookie.php for setcookie is:
$name = "mySite";
$value = "stackoverflow";
setcookie($name, $value, time() + (86400 * 30), "/");
echo $name."=".$value;
with my ajax function mySite=stackoverflow
show in my page but cookie not set in browser. why?
i have ajax function like below:
$.ajax({
url:"cookie.php",
type: 'post',
data: {'ok': val},
success:function(data) {
alert(data);
}
});
and my cookie.php for setcookie is:
$name = "mySite";
$value = "stackoverflow.";
setcookie($name, $value, time() + (86400 * 30), "/");
echo $name."=".$value;
with my ajax function mySite=stackoverflow.
show in my page but cookie not set in browser. why?
1 Answer
Reset to default 10Cookies are set using the HTTP Set-Cookie
header, sent in the HTTP response when a page first loads.
This header instructs the browser to store the cookie and send it back in future requests to the server.
When you set the cookie with ajax, the browser doesn't reload the current page, and no new headers are sent.
Instead, a new request is sent in the background with XMLHttpRequest
, and the cookies are never added to the current page headers, as that page newer reloads and receives the header containing the cookie.
You have to reload the page and get a new set of headers to see the new cookies added in PHP.
There's also the option of setting the cookies in javascript, then they would be visible in the browser right away.
document.cookie="mySite=stackoverflow.; expires=Thu, 18 Dec 2015 12:00:00 UTC; path=/";
本文标签: javascripthow can set cookie with AjaxjQueryStack Overflow
版权声明:本文标题:javascript - how can set cookie with AjaxjQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741630342a2389324.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论