为什么同一个请求在Postman中正常运行,使用Axios发送时却失效?
Axios请求错误修复方案
- 请求头配置字段拼写错误:axios 官方要求的请求头配置字段为
headers(复数形式),你代码中使用了单数header,导致配置的Content-Type、auth-token都不会被挂载到实际发出的请求上,后端无法拿到鉴权信息,直接返回错误。 auth-token值格式错误:你代码中"auth-token": { token }的写法会给请求头传入一个对象类型的值,而接口要求的是纯token字符串,正确写法为"auth-token": token。Postman中你直接填写的是token字符串,所以请求可以正常执行。
修正后代码
await axios({ method: "post", url: `https://ecommersappbytim.herokuapp.com/info/${userId}/update-user`, headers: { "Content-Type": "application/json", "auth-token": token, }, data: { firstName: userInfo.fName, lastName: userInfo.lName, email: userInfo.email, password: userInfo.password, }, })
额外说明:当前主流版本的axios发送JSON格式的POST请求时,会自动设置Content-Type: application/json,无需手动配置该请求头。
内容的提问来源于stack exchange,提问作者Tim Taylor
相关产品推荐
相关产品推荐

