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

当我 curl 时:

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对象吗?

最佳答案

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

我只需要放

Access-Control-Expose-Headers: Location

对于我的响应 header ,它可以正常工作,因此现在我可以使用来访问Location值:
response.headers.get('Location');

约翰!

10-07 19:27
查看更多