關於vue- axios的post請求到后台數據為空的兩種解決方法
一個Request Payload,一個Form Data。
將Request Payload 轉為Form Data格式就可以了,有三種方式:
一、使用qs(推薦)
首先在你的項目里安裝qs模塊:
npm install qs --save-dev
然后在需要使用的頁面引入一下:
import qs from 'qs'
引入好了之后,把上面的postData用qs轉一下再發送給后台就可以了:
const name =this.loginForm.name;
const password =this.loginForm.password;
const CaptchaId =this.loginForm.CaptchaId;
const verifyValue =this.loginForm.verifyValue;
const CaptchaImage =this.loginForm.CaptchaImage;
const postData = qs.stringify({
name: name,
password: password,
CaptchaId: CaptchaId,
verifyValue: verifyValue,
CaptchaImage: CaptchaImage,
});
console.log("前端提交的表單為:",postData)
const { data:res } =await this.$http.post("auth/login",postData)
這樣發送給后台時就是Format Data格式了。