在CORS请求上访问响应头 [英] Accessing response headers on CORS request
问题描述
我正在使用访存API发出类似于以下代码段的跨域请求
I'm using the fetch API to make a cross-domain request similar to the below snippet
window.fetch('http://data.test.wikibus.org/magazines', { method: 'get'})
.then(function(response) {
var linkHeader = response.headers.get('Link');
document.querySelector('#link-header').innerText = 'The Link header is: ' + linkHeader;
});
<span id="link-header"></span>
如您所见,尽管Link头(以及其他一些头)在响应中返回,但仍不可访问。我认为这是一个CORS问题,因为在本地请求中所有标头都是可以访问的。
As you see the Link header (and some other headers too) is not accessible although it is returned in the response. I assume that's a CORS issue, because on local requests all headers are accessible.
这是设计使然吗?
推荐答案
您请求的资源很可能缺少 Access- Control-Expose-Headers
标头,其中包含 Link
作为值。
The resource you are requesting most likely lacks a Access-Control-Expose-Headers
header that contains Link
as value.
请参见 https://fetch.spec.whatwg.org/#http-access-control-expose-headers ,请参见 https://fetch.spec.whatwg.org/#concept-filtered-response -cors 获取从CORS响应中过滤出哪些标头的详细信息。
See https://fetch.spec.whatwg.org/#http-access-control-expose-headers and see https://fetch.spec.whatwg.org/#concept-filtered-response-cors for the details of which headers get filtered out of a CORS response.
这篇关于在CORS请求上访问响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!