vue axios的get和post请求(后台是java代码)

首先get请求例子1:

后台的java代码如下

@RequestMapping("/get2")
public String getOne2(@RequestParam String name){
    return  name +"------------------success+++++++++++get请求";
}

前台的方式一:可以直接在url上面加字符串参数,后台可以愉快的接受

get1() {
  this.$axios.get(
    'http://localhost:8083/get2?name=' + this.name
  )
    .then(function (response) {
      console.log(response.data);
    })
    .catch(function (error) {
      console.log(error);
    });
}

前台的方式二:在get的参数config里面添加params来实现参数的传递后台也可以接受,如下面

get2() {
  this.$axios.get(
    'http://localhost:8083/get2', {
      params: {name: this.name} // 
    }).then(function (response) {
    console.log(response.data);
  })
    .catch(function (error) {
      console.log(error);
    });
},

 post 方式一:通过qs 方式来实现,首先要安装qs插件,要注意JSON 和qs的区别,不明白的可以百度,后台也可以接收

get3() {
  let data = {name: this.name};
  let nam = Qs.stringify(data);
  console.log(nam);
  this.$axios.post(
    'http://localhost:8083/get2',
    nam
  ).then(function (response) {
    console.log(response.data);
  })
    .catch(function (error) {
      console.log(error);
    });
}// 此方法对应上面的java后台 的 方法,可以愉快的调用。

post方法二:首先对应的java方法是下面的get1,注意此处的 @RequestBody和上面的 @RequestParam不一样的

@RequestMapping("/get1")
public String  getOne(@RequestBody User map){
    String name = map.getName();
    return  name+"----------------success+++ post 请求";
}

所以此处的前端代码是

get5() {
  let data = {
    name: this.name
  };
  this.$axios.post(
    'http://localhost:8083/get1',
    data
  ).then(function (res) {
    console.log(res.data);
  }).catch(function (error) {
    console.log(error);
  })
}

这样也可以调用成功。

最后下面的方法,get ,post 都可以使用,但是参数不一样,这里是get的例子,使用post的时候仅仅把参数修改下就可以了。

func() {
  this.$axios({
    url: "http://localhost:8083/get2",
    methods: "get",//post
    params: {  
      name: this.name
    }
  }).then(function (response) {
    console.log(response.data);
  })

}

基本就这样了,希望大家周末愉快!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值