为什么不能在fetch的响应中找到所有标头? [英] Why not all headers can be found in fetch's response?

查看:78
本文介绍了为什么不能在fetch的响应中找到所有标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拦截 Set-Cookie

I'm trying to intercept Set-Cookie response header from fetch's response:

fetch('https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png', {
	method: 'get'
}).then(function(response) {
    for (var pair of response.headers.entries()) {
        console.log(`${pair[0]}: ${pair[1]}`);
    }
});

但是并不是所有的标题(在开发者工具的网络中都可以看到)都可以在其中找到!这是为什么?有什么办法可以获取想要的标题吗?

But not all of the headers (as can be seen in developer tools' network) can be found in there! Why is that? Is there any way I can get the header I'm looking for?

为澄清起见,我不是在查找cookie,而是希望知道何时发送 Set-Cookie 标头.

Just for clarification, I'm not looking for the cookie but I'm interested to know when the Set-Cookie header is sent.

推荐答案

您不能读取 Set-Cookie 标头,因为它被声明为禁止标头.github上的 fetch polyfill提供了合理的解释:

You cannot read the Set-Cookie header as it is declared as forbidden. The fetch polyfill on github provides a reasonable explanation:

与XMLHttpRequest一样,从以下方法返回的Set-Cookie响应标头服务器是禁止的标头名称,因此不能以编程方式使用response.headers.get()进行读取.相反,它是浏览器处理新设置的Cookie的责任(如果适用于当前网址).除非它们是仅HTTP的,否则新的cookie将可通过document.cookie获得.

Like with XMLHttpRequest, the Set-Cookie response header returned from the server is a forbidden header name and therefore can't be programatically read with response.headers.get(). Instead, it's the browser's responsibility to handle new cookies being set (if applicable to the current URL). Unless they are HTTP-only, new cookies will be available through document.cookie.

https://github.com/github/fetch#receiving-cookies

这篇关于为什么不能在fetch的响应中找到所有标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆