如何从浏览器中的提取请求中获取标头位置值 [英] How to get Header Location value from a Fetch request in browser
问题描述
从ReactJS-Redux前端应用程序中,我尝试获取REST API响应的Location Header值.
From a ReactJS - Redux front app, I try to get the Location Header value of an REST API response.
当我卷曲时:
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中进行抓取
When I make a Fetch in ReactJS
var url = 'https://url.company.com/api/v1.0/tasks/'
fetch(url, {
method: 'post',
credentials: 'omit',
headers: {
'Authorization': `Bearer ${token}`
}
}
)
响应对象中没有任何标题: 提取请求中没有标头
I don't have any Headers in the response object : No header in Fetch request
我尝试了在 https中找到的所有response.headers函数. ://developer.mozilla.org/en-US/docs/Web/API/Headers :
response.headers.get('Location');
但是,由于标题为空,所以结果为空.
But well, as headers is empty, I have empty results.
您知道为什么我无法获得填充有标题值的适当的Header对象吗?
Do you know why I can't get a proper Header object filled with the headers values ?
推荐答案
感谢约翰,我找到了答案.
Thanks to John, I've found the answer.
我只需要放
Access-Control-Expose-Headers: Location
对于我的响应标题,它可以正常工作,因此现在我可以使用:
To my response headers and it worked, so now I can access Location value with :
response.headers.get('Location');
约翰·约翰(Thx John)!
Thx John !
这篇关于如何从浏览器中的提取请求中获取标头位置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!