我正在尝试使用 vue.js
在项目中配置 header 和 axios
调用需要 json 的服务。我的问题是,当我使用 POST
调用电话时方法,axios
提出请求Content-Type
带有 x-www-urlencoded
的标题,但在我的代码中,我手动输入了 Content-Type
带有 application / json
的标题.
var loginObj = {
var1: payload.login,
var2: payload.password
}
const jsonLogin = JSON.stringify(loginObj)
const config = {
headers: {
'content-type': 'application/json',
'Accept': 'application/json'
}
}
axios({
url: 'url/example',
method: 'post',
data: jsonLogin,
config
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
})
最佳答案
Axios设置标题配置的属性应该是 headers:
你正在设置config:
尝试:
axios({
url: 'url/example',
method: 'post',
data: jsonLogin,
headers: config.headers
})
或将您的 const 配置更改为:
const configHeaders = {
"content-type": "application/json",
"Accept": "application/json"
};
并将其用于:
axios({
url: "url/example",
method: "post",
data: jsonLogin,
headers: configHeaders
});
关于javascript - axios中的Header Content Type无法设置application/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57334092/