x-access-token 未添加到 POST 请求
x-access-token not added to POST request
我在请求或创建资源时使用来自 Redux 操作的 Axios 调用来处理用户 authentication/authorization。我已经成功完成了所有 creating/storing/signing 的令牌。当用户对资源发出 GET 请求时,header 将像这样传递:
return axios.get('applicants/', { headers: authHeader() }).then(result => {
const applicants = []
result.data.forEach(item => {
applicants.push(item)
})
而且效果很好。 header 包含应有的 'x-access-token'。当我像这样制作 POST 时不起作用:
return axios.post('applicants/create', { headers: authHeader() })
.then(result => {
dispatch(_addApplicants(result.data))
})
'x-access-token' 未包含在 header 中。同一个函数为两个路由提供了令牌,所以我确信它与不同的 HTTP 动词有关。有人有什么想法吗?
Header 在 GET 上:
{
host: 'localhost:3000',
connection: 'keep-alive',
accept: 'application/json, text/plain, */*',
'x-access-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVmNGJkMTRhMzcwOGNiNTViMDhmOGM1ZCIsImlhdCI6MTU5ODgwOTI0OCwiZXhwIjoxNTk4ODk1NjQ4fQ.7k4xi3kPo-aKyOf_5GNVv3pjO_WsIZwg8Ja1MgwfJCg',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
origin: 'http://localhost:8080',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
referer: 'http://localhost:8080/',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'if-none-match': 'W/"2-l9Fw4VUO7kr8CvBlt4zaMCqXZ0w"'
}
Header POST:
{
host: 'localhost:3000',
connection: 'keep-alive',
'content-length': '204',
accept: 'application/json, text/plain, */*',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
'content-type': 'application/json;charset=UTF-8',
origin: 'http://localhost:8080',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
referer: 'http://localhost:8080/',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9'
}
您将 headers 作为第二个参数传递给 axios.post
,但实际上它应该是第三个参数。第二个参数是 data
.
axios.post(
"/applicants/create",
{}, // add empty object or null here as second argument
{
headers: authHeader()
}
})
我在请求或创建资源时使用来自 Redux 操作的 Axios 调用来处理用户 authentication/authorization。我已经成功完成了所有 creating/storing/signing 的令牌。当用户对资源发出 GET 请求时,header 将像这样传递:
return axios.get('applicants/', { headers: authHeader() }).then(result => {
const applicants = []
result.data.forEach(item => {
applicants.push(item)
})
而且效果很好。 header 包含应有的 'x-access-token'。当我像这样制作 POST 时不起作用:
return axios.post('applicants/create', { headers: authHeader() })
.then(result => {
dispatch(_addApplicants(result.data))
})
'x-access-token' 未包含在 header 中。同一个函数为两个路由提供了令牌,所以我确信它与不同的 HTTP 动词有关。有人有什么想法吗?
Header 在 GET 上:
{
host: 'localhost:3000',
connection: 'keep-alive',
accept: 'application/json, text/plain, */*',
'x-access-token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVmNGJkMTRhMzcwOGNiNTViMDhmOGM1ZCIsImlhdCI6MTU5ODgwOTI0OCwiZXhwIjoxNTk4ODk1NjQ4fQ.7k4xi3kPo-aKyOf_5GNVv3pjO_WsIZwg8Ja1MgwfJCg',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
origin: 'http://localhost:8080',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
referer: 'http://localhost:8080/',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'if-none-match': 'W/"2-l9Fw4VUO7kr8CvBlt4zaMCqXZ0w"'
}
Header POST:
{
host: 'localhost:3000',
connection: 'keep-alive',
'content-length': '204',
accept: 'application/json, text/plain, */*',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36',
'content-type': 'application/json;charset=UTF-8',
origin: 'http://localhost:8080',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
referer: 'http://localhost:8080/',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9'
}
您将 headers 作为第二个参数传递给 axios.post
,但实际上它应该是第三个参数。第二个参数是 data
.
axios.post(
"/applicants/create",
{}, // add empty object or null here as second argument
{
headers: authHeader()
}
})