解决方法:
设置请求头
headers: {
"Content-type": "application/json;charset=UTF-8",
},
一、axios
get请求:
//axios GET请求添加数据 方法一:
this.axios({
method: "get",
url: "/houtai/book/add",
headers: {
"Content-type": "application/json;charset=UTF-8",
},
params: {
author: this.author,
book: this.book,
price: this.price,
},
})
.then((res) => {
this.success = true;
console.log(res);
})
.catch((error) => {
this.error = true;
console.log("error:" + error);
});
post请求:
//axios的POST添加数据 方法一:
this.axios({
method: "post",
url: "/houtai/book/postAdd",
headers: {
"Content-type": "application/json;charset=UTF-8",
},
data: {
author: this.author,
book: this.book,
price: this.price,
},
})
.then((res) => {
this.success = true;
console.log(res);
})
.catch((error) => {
this.error = true;
console.log("error:" + error);
});
二、axios.post
var data = { author: this.author, book: this.book, price: this.price };
this.axios.post("/houtai/book/postAdd",data, {
headers: {
"Content-type": "application/json;charset=UTF-8",
},
})
.then((res) => {
console.log(res);
this.success=true;
})
.catch((error)=>{
this.error=true;
console.log(error);
})
三、axios.get
//axios GET请求添加数据 方法二
this.axios
.get("/houtai/book/add", {
params: {
author: this.author,
book: this.book,
price: this.price,
},
headers: {
"Content-type": "application/json;charset=UTF-8",
},
})
.then((res) => {
console.log(res);
this.success = true;
})
.catch((error) => {
this.error = true;
console.log("error:" + error);
});