axios传值的方式

get 方式

this.$axios
  .get("http://localhost:8081/users/", {
    // data()中的queryInfo对象
    params: this.queryInfo
  })
  .then(res => {
    if (res.data.code === 200) {
    }
  });

post 方式

  • 方式一:但是兼容性不好,在 ie 浏览器没用
var params = new URLSearchParams();
params.append("name", this.addUserForm.name);
// params.append("userInfo", this.addUserForm);尝试拼接对象,但是在f12后的xhr中,传值仅仅显示是对象,后端拿不到值
this.$axios
  .post("http://localhost:8081/users/add?", params)
  .then(response => (this.Datas = response.data.json.userMaps));
  • 方式二:
    在 main.js 中引入 axios 中自带的:

import Qs from ‘qs’
Vue.prototype.qs = Qs

const params = this.qs.stringify({
  id: this.addUserForm.id,
  name: this.addUserForm.name
});
this.$axios
  .post("http://localhost:8081/users/add", params)
  .then(success => {
    if (success.data.code === 200) {
      this.editDialogVisible = false;
      this.getUserList();
    }
  })
  .catch(err => {
    this.$message.error("添加失败:" + err);
  });

put 方式

方式一:直接传值,不推荐
这种是前面怎么修改,值后端都是不能获取到,但是现在可以跟 post 的方式二一样。

this.$axios
  .put(
    `http://localhost:8081/users/changeStatus?id=${userInfo.id}&status=${userInfo.status}`
  )
  .then(res => {
    if (res.data.code === 200) {
      this.$message.success("更新状态成功");
      // 重新获取数据
      this.getUserList();
    }
  })
  .catch(err => {
    return this.$message.error(`更新状态失败:${err}`);
  });

delete 方式

this.$axios
  .delete("http://localhost:8081/users/delete", {
    params: {
      userId: userId
    }
  })
  .then(success => {
  });

注意:前端怎么传值,后端就用什么拿值,一一对应。
更多内容,请点击下方:

My Blog: http://coderblue.cn/
Github:https://github.com/onecoderly
My Project:http://coderblue.cn/project/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值