doqs8936 2018-04-06 19:53
浏览 52

无法读取用PHP设置的Cookie [关闭]

I have a php that sets:

setrawcookie('psd_susenka', rawurlencode("cookie_value"), time() + (86400 * 365));

I even tried the long version with paths, domains, and switches (http only and secure, both with false values)

then, after reloading the page I have jquery:

$(document).ready(function(){
  alert(document.cookies);
})

If the only cookie set is "psd_susenka" then I get alert with "Undefined". If I set another cookies with $.cookie("test", something, { expires: 365 }); then the alert writes only the cookies set via jquery

So what is wrong with the php code? Or maybe something wrong with the jquery alert? I got <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> loaded and the cookie is manualy checked in chrome so it exists.

  • 写回答

1条回答 默认 最新

  • duanpo7796 2018-04-06 20:49
    关注

    You probably have a typo.

    Try document.cookie instead document.cookies.

    Documentation: https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

    P.S. document.cookie is part of WebAPI (see: https://developer.mozilla.org/en-US/docs/Web/API). This is not part of jQuery and not part of JavaScript. JavaScript (also jQuery, because it works only with JavaScript) is able to work with WebAPI.

    评论

报告相同问题?