abe*_*abe 6 javascript cookies cross-domain fetch-api
下面的代码引用和请求引用有注释,这些注释是在 SO 中添加的,以解释我对原始请求的理解,并且代码不包含注释。
我知道使用身份验证标头进行获取请求的标准。我需要做的是获取服务器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。<- 不知道相关端口和IP是否不同
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)
Run Code Online (Sandbox Code Playgroud)
发出请求后,浏览器会对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
Run Code Online (Sandbox Code Playgroud)
但是该请求不包含 cookie。
据我所知,它应该将 cookie 与 fetch 请求一起发送。
Cookie 属于一个起源。
Run Code Online (Sandbox Code Playgroud)let cookies = document.cookie console.log(cookies) //this logs the cookie so I know its defo there
这表明 HTML 文档的来源有 cookie。
发出请求后,浏览器会对http://0.0.0.0:8081/write进行选项调用
所以您正在发出跨域请求。
Cookie 不属于该来源,因此浏览器不会发送它们。
如果您想将 cookie 发送到http://0.0.0.0:8081/ ,那么您需要向http://0.0.0.0:8081/发出请求,并让它使用Set-Cookie响应中的标头将它们设置在第一名。浏览器不会设置 到 的 cookie,:8080因为:8081它们不是:8081的 cookie。
通常,Web 服务将使用标Authorization头 而不是cookie。
let otherPram= {
headers: {
"Authorization": "Bearer SomeToken"
},
credentials: 'include', //this is what I need to tell the browser to include cookies
method: "GET"
};
Run Code Online (Sandbox Code Playgroud)
旁白:我删除了"content-type": "application/json". 您正在发出GET请求,因此该请求没有可指定类型的内容。
| 归档时间: |
|
| 查看次数: |
14813 次 |
| 最近记录: |