从axios中的响应头获取数据 [英] Getting data from response headers in axios
本文介绍了从axios中的响应头获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Axios发出POST请求,此调用在响应头和正文中返回数据。在标头中,它返回一个x-auth-token
,我想获取此内标识的值,但它返回:
undefined is not an object
我是这样做的:
axios.post('app.com/api/login', data)
.then(response => {
console.log(response.headers.get("x-auth-token"));
})
.catch(error => {
console.log(error)
});
推荐答案
在Github注释中明确提到了如何检索标头 see
fetchFromServer = async(data) => {
const response = await axios.post(url, data, headers)
console.log(response.headers)
}
如果您可以看到日志中的所有标头,则可以尝试其中任何一个来从响应中获取数据。要检查响应中的可用密钥,您可以尝试
console.log(Object.keys(response.headers))
console.log(response.headers.your_required_key
(例如response.headers.Token)console.log(response.headers["your_required_key"]
如果上述操作失败。(console.log(response.headers[";content-type";])
这篇关于从axios中的响应头获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文