[英]Unable to access Set-cookie response header and/or cookie request header
我正在尝试使用 REST API 登录平台。 关于 REST API 身份验证的平台文档说,在发布凭据后进行身份验证时,该帖子将返回会话 cookie 以在所有后续经过身份验证的请求上发回。 问题是我正在使用跨域,它不允许我访问 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 header 中的值不会在浏览器中自动设置,使用 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。
XHR API 禁止直接访问 cookie 数据。
返回所有响应标头,不包括与 Set-Cookie 或 Set-Cookie2 不区分大小写匹配的标头,作为单个字符串,每个标头行由 U+000D CR U+000A LF 对分隔,不包括状态行,并且每个标头名称和标头值由 U+003A COLON U+0020 SPACE 对分隔。
您要么需要使用其他方法传递数据,要么只需设置withCredentials
并让浏览器在内部处理 cookie。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.