解决vue使用axios发起请求并传递参数后端接收为null的解决方法

第一种方法
在vue项目中更改,使用 new URLSearchParams()构造参数,将参数放到里面,发起请求时直接将params传递到服务器即可

           let params = new URLSearchParams();
           params.append("account",this.loginForm.account)
           params.append("password",this.loginForm.password)
           const {data:res} = await this.$http.post("/user/login",params)

第二中方法
将控制层接收的参数改为Map

    public JsonData loginValid(@RequestBody Map map){
        AccountInfomation accountInfomation = new AccountInfomation();
        accountInfomation.setPassword((String) map.get("password"));
        accountInfomation.setAccount((String) map.get("account"));
        AccountInfomation AccountInfomationVO = acountService.loginValid(accountInfomation);
        if (AccountInfomationVO != null){
            return JsonData.buildSuc(AccountInfomationVO);
        }
        return JsonData.buildError("登录失败");
    }

第三种方法
第三种方法也是最根本的方法,由于axios默认发送数据时,数据格式是Request Payload,而并非我们常用的Form Data格式,后端数据接收不到,所以在发送之前,需要使用qs模块对其进行处理。
首先需要安装qs
npm install qs
然后在main.js中导入并挂载到Vue对象上

import qs from 'qs'
Vue.prototype.$qs = qs

然后·在请求的时候处理一下参数即可

const {data: res} =await this.$http.post("/student/saveStudentInfo",this.$qs.stringify(this.saveStudentRuleForm))
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值