我正在尝试破坏axios.get请求的response.headers,因为我只需要x-csrf-token。它始终是第二位置。这是res.headers响应的外观
{
'content-type': 'application/json; charset=utf-8',
'x-csrf-token': '17hoqAWEwVmcN32wYgN9WA==',
'cache-control': 'no-cache, no-store',
dataserviceversion: '2.0',
'c4c-odata-response-time': '1195 ms',
date: 'Fri, 28 Feb 2020 10:06:55 GMT',
'transfer-encoding': 'chunked',
connection: 'close, Transfer-Encoding',
'set-cookie': [
'sap-usercontext=sap-client=041; path=/;HttpOnly;Secure',
'
],
'strict-transport-security': 'max-age=31536000 ; includeSubDomains'
}
我尝试过
let{a,b} = res.headers;
console.log(b);
和
let[,b] = res.headers;
console.log(b);
但是我得到:undefined不是一个函数
总是第二个位置
与对象分解无关紧要。您使用键,而不是位置。
获得它:
const {'x-csrf-token': token} = res.headers;
这是解决问题的方法
let [,b] = Object.entries(res.headers);