【发布时间】:2017-09-15 22:04:11
【问题描述】:
我想检索服务器发送的响应标头。但我得到了空的对象。我可以在 chrome 开发工具中看到响应标头,但无法从 javascript 中获取它们。
(我正在为 xhr 请求使用 isomorphic-fetch lib。)
fetch("http://my.cors.url/postsomedata", {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(myjsobject),
})
.then(response => response.headers)
.then((headers) => {
console.log(headers); // empty object
})
这不是重复的 How to get Response headers in AJAX 我没有使用 jQuery。
【问题讨论】:
-
如果你不能从
response.headers得到它,你就不能得到它。这听起来像是一个 CORS 问题。 -
@KevinB,但为什么我可以在 chrome 开发工具中看到它们?
-
因为这些是开发工具。开发工具中有很多东西是您无法通过 javascript 访问/执行的。
标签: javascript ajax cors isomorphic-fetch-api