如何从浏览器中的 Fetch 请求中获取 Header 位置值
How to get Header Location value from a Fetch request in browser
从 ReactJS - Redux 前端应用程序,我尝试获取 REST API 响应的位置 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 中进行 Fetch 时
var url = 'https://url.company.com/api/v1.0/tasks/'
fetch(url, {
method: 'post',
credentials: 'omit',
headers: {
'Authorization': `Bearer ${token}`
}
}
)
我的回复 object 中没有任何 Header:
No header in Fetch request
我尝试了在 https://developer.mozilla.org/en-US/docs/Web/API/Headers 中找到的所有 response.headers 函数:
response.headers.get('Location');
但是,由于 headers 是空的,所以我的结果也是空的。
你知道为什么我不能得到一个正确的 Header object 填充 headers 值吗?
感谢约翰,我找到了答案。
我只需要输入
Access-Control-Expose-Headers: Location
根据我的回复 headers 并且有效,所以现在我可以通过以下方式访问 Location 值:
response.headers.get('Location');
谢谢约翰!
除了在服务器中公开 Location Header。
我只能通过以下方式访问 React 应用程序中的位置:
response.headers.location;
HttpRequest req = new HttpRequest();
req.setHeader('Access-Control-Allow-Origin', '*');
req.setHeader('Access-Control-Allow-Credentials', 'true');
response.getHeader('location'); //不要使用 response.getHeader('Location');
//这可能会导致问题。
从 ReactJS - Redux 前端应用程序,我尝试获取 REST API 响应的位置 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 中进行 Fetch 时
var url = 'https://url.company.com/api/v1.0/tasks/'
fetch(url, {
method: 'post',
credentials: 'omit',
headers: {
'Authorization': `Bearer ${token}`
}
}
)
我的回复 object 中没有任何 Header: No header in Fetch request
我尝试了在 https://developer.mozilla.org/en-US/docs/Web/API/Headers 中找到的所有 response.headers 函数:
response.headers.get('Location');
但是,由于 headers 是空的,所以我的结果也是空的。
你知道为什么我不能得到一个正确的 Header object 填充 headers 值吗?
感谢约翰,我找到了答案。
我只需要输入
Access-Control-Expose-Headers: Location
根据我的回复 headers 并且有效,所以现在我可以通过以下方式访问 Location 值:
response.headers.get('Location');
谢谢约翰!
除了在服务器中公开 Location Header。
我只能通过以下方式访问 React 应用程序中的位置:
response.headers.location;
HttpRequest req = new HttpRequest();
req.setHeader('Access-Control-Allow-Origin', '*');
req.setHeader('Access-Control-Allow-Credentials', 'true');
response.getHeader('location'); //不要使用 response.getHeader('Location'); //这可能会导致问题。