简体   繁体   English

jQuery Cookie插件似乎并没有真正设置Cookie

[英]jQuery Cookie plugin doesn't really seem to be setting a cookie

My code sets a cookie with javascript (using the cookie plugin for jQuery), then makes an ajax call to a php script that needs to read that cookie. 我的代码使用javascript设置了一个cookie(使用jQuery的cookie插件),然后对需要读取该cookie的php脚本进行了ajax调用。 It appears that the jQuery cookie plugin has a bug, because the PHP script isn't able to read this cookie. jQuery cookie插件似乎有一个错误,因为PHP脚本无法读取此cookie。 Furthermore, when I look in the Resources panel of Chrome, I don't see the cookie under the relevant domain. 此外,当我在Chrome的“资源”面板中查看时,在相关域下看不到Cookie。 Additionally, if I set the cookie using the standard document.cookie method of Javascript, the cookie DOES appear in the Resources panel and my PHP script is able to read it. 另外,如果我使用Javascript的标准document.cookie方法设置cookie,则cookie确实会出现在“资源”面板中,并且我的PHP脚本能够读取它。 The main reason I want to use the jQuery plugin is because the cookie value I'm setting has line breaks, and the jQuery cookie plugin seems to handle that elegantly. 我要使用jQuery插件的主要原因是因为我设置的cookie值有换行符,而jQuery cookie插件似乎可以很好地处理它。 It's a value that is going to be stored in a MySQL database, and those line breaks will be automatically converted to (br) and (p) tags when output. 这个值将存储在MySQL数据库中,并且这些换行符将在输出时自动转换为(br)和(p)标记。 So the jQuery cookie plugin seems to give me the value I want... but not in a real cookie! 因此,jQuery Cookie插件似乎为我提供了我想要的值...但不是真正的cookie!

Anyway, here's the code I'm using. 无论如何,这是我正在使用的代码。 Feel free to just tell me how to maintain the linebreaks I need without using the jQuery cookie plugin rather than trying to figure out what's going wrong with the plugin/my implementation of the plugin. 随意告诉我如何在不使用jQuery cookie插件的情况下维护所需的换行符,而不是试图弄清楚插件/我的插件实现到底出了什么问题。

Javascript: Javascript:

$.cookie('flavor','chocolate',{expires:7});
$.get('http://example.com/readCookie.php',function(data){
    console.log(data);
});

readCookie.php readCookie.php

$myCookie = $_COOKIE['flavor'];
echo $myCookie;

The 'echo' statement above doesn't echo anything at all, not even an empty string. 上面的'echo'语句根本没有回显任何内容,甚至没有空字符串。 If I try var_dump with the cookie, I get NULL. 如果我尝试将cookie与var_dump一起使用,则会得到NULL。 I can, however, enter $.cookie('flavor') into the console and see that it was in fact set. 但是,我可以在控制台中输入$ .cookie('flavor')并看到它实际上已设置。 But as I said before, it doesn't appear in the Resources panel of Chrome's developer tools, so I'm not convinced that it is actually a real cookie... and my PHP script seems to agree with that doubt as well :) 但是正如我之前说过的那样,它没有出现在Chrome开发人员工具的“资源”面板中,所以我不相信它实际上是一个真正的cookie ...而且我的PHP脚本似乎也同意这个疑问:)

In the code you posted, you set a cookie named 'flavor' on the client side, and try to read a cookie named 'comment' at the server. 在发布的代码中,您在客户端设置了一个名为“ flavor”的cookie,并尝试在服务器上读取一个名为“ comment”的cookie。 Makes sense to me that you're not getting anything. 对我来说,你什么都没有得到。

Perhaps that's just an error on your part in posting the question. 也许那只是您发布问题时的错误。

So what happens when you point the AJAX request at 那么,当您将AJAX请求指向时会发生什么

'http://' + window.location.host + '/readCookie.php'

Or even at just 甚至只是

'/readCookie.php'

Cookies are domain specific. Cookies是特定于域的。

I suspect the line breaks in the cookie value may well be causing the problems. 我怀疑Cookie值中的换行符很可能是造成问题的原因。

Why not base64 encode the cookie value? 为什么不对base64编码cookie值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM