Vue中使用axios分别实现post方式和get方式提交数据

1.安装axios

npm install --save axios

2.在main.js中引入并使用该模块,并设置域名(以后的请求都将基于该域名)

import axios from 'axios'



Vue.prototype.$http=axios.create({
  baseURL: 'https://www.longdatech.com',
})

3.使用post提交数据并获取返回结果

this.$http.post("/uset/login",{
  userName:'admin',
  password:'123'
}).then(res=>{
  console.log("返回结果:" + res);
  if(res.code==0){
    that.$message.success('登录成功')
  }else {
    that.$message.error('登录失败')
  }
}).catch(error=>{
  that.$message.error('登录失败')
})

4.使用get提交数据并获取返回结果

this.$http.get("/user/getUserInfo",{
  params:{
    id:'1'
  }
}).then(res=>{
  if(res.code==0){
    that.$message.success('查询成功')
  }else {
    that.$message.error(res.message||'查询失败')
  }
}).catch(error=>{
  that.$message.error('查询失败')
})

5.请求其他域名下的接口

this.$http.get("http://127.0.0.1:8888/user/check?id=1").then(function(res){
  // console.log(res.data);
},function(res){
  // console.log(res);
});

如对上述使用有疑问,可联系本人qq:2370775541

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值