关于vue- axios的post请求到后台数据为空的两种解决方法


关于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格式了。