简体   繁体   English

如何使用javascript从响应访问set-cookie

[英]How to access set-cookie from response using javascript

I sent a cookie from server side 我从服务器端发送了一个cookie

ResponseBuilder response = Response.ok( (Object) file );
response.header( "Set-Cookie", "fileDownload=test");

I can already view it under the ' Response Header ' of the f12 developer tools console. 我已经可以在f12开发人员工具控制台的“ 响应标头 ”下查看它。 However, document.cookie returns an empty string. 但是,document.cookie返回一个空字符串。 The HttpOnly attribute is also unchecked. HttpOnly属性也未选中。 I'm also not in incognito mode and have cookies enabled. 我也没有进入隐身模式,并启用了Cookie。 I've already tried changing the localhost address in case it was a web server problem. 我已经尝试过更改本地主机地址,以防它是Web服务器问题。 Is there a way to check the cookie with java script? 有没有办法用Java脚本检查Cookie? I just need to check if it has been passed. 我只需要检查它是否已通过。

Fixed by adding "path" & "domain" parameter. 通过添加“路径”和“域”参数来解决。

credits to Andres @bismarck Not sure. 致谢Andres @bismarck。 But you would probably need to specify the "path" & "domain" parameter. 但是您可能需要指定“ path”和“ domain”参数。 For example / as path and "" as domain. 例如,/作为路径,“”作为域。 Please check this stackoverflow.com/questions/1134290/… – Andrés 请检查此stackoverflow.com/questions/1134290/…–Andrés

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

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