【发布时间】:2020-03-06 11:49:43
【问题描述】:
下面的代码参考和请求参考有 cmets 这些是在 SO 中添加的,以解释我对原始请求和代码不包含 cmets 的理解。
我知道使用 auth 标头获取请求的标准。我需要做的是获取服务器 A 设置给服务器 B 的 cookie,而无需通过 javascript 传递它。
我有服务器 A:http://127.0.0.1:8080 包含 index.html
查看 cookie jar 时的 index.html 包含一个 cookie
我还有服务器 B:http://0.0.0.0:8081。
http://127.0.0.1:8080/index.html 发出以下请求
let url = "http://0.0.0.0:8081/write" //this url is o a different server so certain headers are needed
let cookies = document.cookie
console.log(cookies) //this logs the cookie so I know its defo there
let otherPram= {
credentials: 'include', //this is what I need to tell the browser to include cookies
method: "GET"
};
fetch(url, otherPram)
在发出请求后,浏览器向http://0.0.0.0:8081/write 发出选项调用,响应为:
access-control-allow-credentials: true
access-control-allow-headers: accept, authorization, content-type, origin, x-requested-with, access-control-allow-credentials, cookie, access-control-allow-origin
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-origin: http://127.0.0.1. //also tried this with http://127.0.0.1:8080
access-control-expose-headers: Cache-Control, Content-Language, Content-Type, cookie
access-control-max-age: 600
connection: keep-alive
content-length: 0
date: Thu, 16 Jan 2020 08:22:19 GMT
但是请求不包含 cookie。
据我所知,它应该随 fetch 请求一起发送 cookie。
【问题讨论】:
标签: javascript cookies cross-domain fetch-api