【发布时间】:2021-06-01 05:30:57
【问题描述】:
我正在发送 axios post 请求,但浏览器首先发送选项请求。为了阻止这种情况发生,我创建了一个如下所示的配置对象:
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
在我的应用程序的一部分中,它运行得非常好。这里是:
await this.$http.post("/editJob",this.job,config).then(response =>{
let job = response.data;
if(job){
job = Job.convertObject(job);
this.$store.commit("editJob",job);
}
}).catch(err =>{
console.log(err);
this.errMessages.push("There was a problem editing the Job. Contact your administrator.");
})
但是在应用程序的不同部分,我发送了一个稍微复杂的 JS 对象,它仍然发送选项请求。这里是:
await this.$http.post("/addUser",this.user,config).then(response =>{
let user = User.convertObject(response.data);
if(user){
this.$store.commit("addUser",user);
}else{
this.errMessages.push("Unable to add the Employee. Please contact your administrator.");
}
}).catch(err =>{
console.log(err);
this.errMessages.push("Unable to add the Employee. Please contact your administrator.");
});
后端清楚地记录了第二种情况下的 OPTION 请求,但我看不出 axios 调用有什么不同。我目前束手无策。如果有人有建议,请提前告诉我,谢谢。
迈克
【问题讨论】: