如何从浏览器中的提取请求中获取标题位置值

问题描述 投票:4回答:1

从ReactJS-Redux前端应用程序,我尝试获取REST API响应的Location Header值。

当我卷曲时:

curl -i -X POST -H "Authorization: Bearer MYTOKEN" https://url.company.com/api/v1.0/tasks/

我有这个答案:

HTTP/1.1 202 ACCEPTED
Server: nginx
Date: Fri, 12 Aug 2016 15:55:47 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Location: https://url.company.com/api/v1.0/tasks/status/idstatus

当我在ReactJS中进行抓取时

    var url = 'https://url.company.com/api/v1.0/tasks/'
    fetch(url, {
            method: 'post',
            credentials: 'omit',
                headers: {
                    'Authorization': `Bearer ${token}`
                }
            }
     )

我在响应对象中没有任何标题:No header in Fetch request

我尝试了在https://developer.mozilla.org/en-US/docs/Web/API/Headers中找到的所有response.headers函数:

response.headers.get('Location');

但是,由于标题为空,所以结果为空。

您知道为什么我无法获得填充有标题值的适当的Header对象吗?

http-headers fetch-api
1个回答
7
投票

感谢约翰,我找到了答案。

我只好放了

Access-Control-Expose-Headers: Location

对于我的响应标题,它起作用了,所以现在我可以使用:]访问位置值。

response.headers.get('Location');

约翰,谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.