javascript 使用 Ajax 请求设置 Cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21211015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Cookie with Ajax Request
提问by Pippa Rose Smith
I have a php page with a form, that has a button. When the button is clicked, a jquery function is run, which performs some validation tests, then submits the form using ajax. In the php script run by ajax, a cookie is set. Immediatly after the cookie is set, I then try to get the cookie value, which I echo from the script and spit out in the success function of the ajax request. The cookie value hasn't set.
我有一个带有表单的 php 页面,它有一个按钮。单击按钮时,会运行一个 jquery 函数,该函数执行一些验证测试,然后使用 ajax 提交表单。在ajax运行的php脚本中,设置了一个cookie。设置 cookie 后,我立即尝试获取 cookie 值,该值从脚本中回显并在 ajax 请求的成功函数中吐出。cookie 值尚未设置。
Code is as follows.
代码如下。
mainpage.php
主页.php
<script type="text/javascript">
$( document ).ready(function()
{
$('#submit_compare_stage1').click(function()
{
//Validation stuff
if (passed_validation)
{
var form_data = $('#compare_form').serialize(); //Collect query details into a form
$.ajax({
type: "POST",
url: "scripts/process_compare.php",
data: form_data,
success: function(data)
{
alert(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
//Error stuff
}
});
}
return false;
});
});
</script>
<form name="compare_form" id="compare_form" action="">
....
<button id='submit_compare_stage1' name='submit_compare_stage1'>Next</button>
</form>
process_compare.php
process_compare.php
<?php
//MySQL Stuff
makeCookie('cs', '2');
echo 'hi' . getCookie('cs', false);
echo "success";
function makeCookie($name, $contents, $length = 3600)
{
// Create a new cookie
setcookie($name, $contents, time() + $length, '/');
}
function getCookie($name, $delete = true)
{
// Return the contents of a cookie and delete it if requested
$contents = $_COOKIE[$name];
if($delete) {
setcookie($name, '', time() - 3600, '/');
setcookie($name, '', time() - 3600);
}
return $contents;
}
?>
The ajax request is posting alert messages saying "hisuccess" so the cookie isn't being set.
ajax 请求正在发布警告消息,提示“成功”,因此未设置 cookie。
I'm not sure if it's because the page needs refreshing or something else, but I do know the code used to work when we had a regular submit the form using action="/process_compare.php" and an iframe to put results into.
我不确定是因为页面需要刷新还是其他原因,但我知道当我们使用 action="/process_compare.php" 和一个 iframe 定期提交表单时,代码可以正常工作。
Can anyone help?
任何人都可以帮忙吗?
回答by Olaf Dietsche
A cookie is sent as a header of the response. In the data
argument, you get the body of the response, not the headers.
cookie 作为响应的标头发送。在data
参数中,您获得响应的正文,而不是标题。
It seems, that the headers of the response, you get from getResponseHeader()
or MDN - getAllResponseHeaders()
, do not include the Set-Cookie
headers.
看来,您从getResponseHeader()
或获得的响应标头MDN - getAllResponseHeaders()
不包括Set-Cookie
标头。
But the cookies are handled transparently by the browser. So, in order to access the cookies, you can use the document.cookie
property
但是 cookie 是由浏览器透明处理的。因此,为了访问 cookie,您可以使用该document.cookie
属性
$.ajax({
...
success: function(data, status, jqxhr) {
var cookies = [];
if (document.cookie)
cookies = document.cookie.split('; ');
// do something with the cookies
},
});
or use the jQuery Cookieplugin available at github.
或使用github 上提供的jQuery Cookie插件。