无法访问Set-cookie响应标头和/或cookie请求标头

时间:2016-03-23 11:10:10

标签: javascript rest cookies request-headers response-headers

我正在尝试使用REST API登录平台。关于REST API身份验证的平台Doc说,在发布凭据后进行身份验证后,帖子将返回会话cookie以发回所有后续经过身份验证的请求。问题是我&# 39; m使用跨域域名,不要让我访问Set-cookie标题或cookie标题。我尝试了很多我在论坛上看到的东西。  这是实际的服务器配置:

Access-Control-Allow-Origin:*;Access-Control-Allow-Methods:GET, POST, OPTIONS;Access-Control-Expose-Headers:Set-Cookie;Access-Control-Allow-Headers:Set-Cookie;HttpOnly:false;

这是我在帖子后使用此配置获得的响应标头:

Access-Control-Allow-Headers:Set-Cookie
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:http://127.0.0.1:4482
Access-Control-Expose-Headers:Set-Cookie
Cache-Control:no-cache, no-store, private
Connection:keep-alive
Content-Length:10
Content-Type:text/plain; charset=utf-8
Date:Wed, 23 Mar 2016 08:51:02 GMT
HttpOnly:false
Server:Wisp/1.0.67.13
Set-Cookie:fanws=071b2016-2718-f680-7149-000d3ab17336-cea3f199fc0dd453;Path=/
X-Frame-Options:SAMEORIGIN

Set-cookie标头中的值不会在浏览器中自动设置,并且似乎无法使用javascript。当我输入:console.log(headers.getAllResponseHeaders());时 我甚至都没有在控制台中看到Set-Cookie。我得到的是:Content-Type: text/plain; charset=utf-8 Cache-Control: no-cache, no-store, private

我也使用了这个配置服务器端:

Access-Control-Allow-Origin:http://127.0.0.1:4482;Access-Control-Allow-Methods:GET, POST, OPTIONS;Access-Control-Expose-Headers:Set-Cookie;Access-Control-Allow-Headers:Set-Cookie;HttpOnly:false;Access-Control-Allow-Credentials:true;

客户端唯一改变的是Set-Cookie从响应头消失,并且请求头中出现了具有相同值的Cookie头:Accept:application/json, text/plain, */* Accept-Encoding:gzip, deflate Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 Connection:keep-alive Content-Length:103 Content-Type:application/x-www-form-urlencoded; charset=UTF-8 Cookie:fanws=071ad3d7-a5ad-4cc0-7ff4-000d3ab17336-d81da7968d36156c Host:******** Origin:http://127.0.0.1:4482 Referer:http://127.0.0.1:4482/Skyspark.html User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36

我仍然遇到同样的问题,因为我无法访问服务器发送的cookie。有谁可以帮助我吗?谢谢。

1 个答案:

答案 0 :(得分:0)

XHR API forbids direct access to cookie data

  

将所有响应标头(不包括与Set-Cookie或Set-Cookie2 不区分大小写匹配的标头)作为单个字符串返回,每个标题行由U + 000D CR U分隔+ 000A LF对,不包括状态行,每个标题名称和标题值由U + 003A COLON U + 0020 SPACE对分隔。

您需要使用其他方法传递数据,或只需set withCredentials并让浏览器在内部处理Cookie。

相关问题