vue发送请求遇到的坑

先看错误信息:
在这里插入图片描述
后台一直接受不到参数 参数缺失

在这里插入图片描述
问题所在

有些时候使用application/x-www-form-urlencoded,也就是大家在项目经常用到的一种提交格式
有些时候使用application/json提交数据,也很常用
本人在开发过程中呢
一般使用application/x-www-form-urlencoded格式提交数据来查询
使用application/json格式来增加数据和修改数据

解决方法

途径1:设置请求头 并且将 对象参数 转换为查询参数类型(qs)

//配置axiox
import axios from 'axios'
axios.defaults.baseURL = 'http://127.0.0.1:8088/'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.transformRequest = [function (data) {
  let ret = ''
  for (let it in data) {
    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  }
  return ret
}]
//挂载vue原型对象
Vue.prototype.$http = axios

然后再发送请求

  submitForm() {
            this.$refs.login.validate( async valid => {
                if (valid) {
					//发起登录请求
					const {data:res} = await this.$http.post("manage/login",this.loginForm);
					console.log(res);
                    this.$message.success('登录成功');
                    localStorage.setItem('ms_username', this.loginForm.username);
                    this.$router.push('/');
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },

请求成功!
在这里插入图片描述

途径2:安装qs工具

1.安装qs,首先cmd开起来,cd到你的项目根目录,执行以下命令

npm install qs --save

2.在main.js中进行导入qs,交给vue进行使用

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

3,请求时将参数

qs.stringify(参数对象)

成功!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值