vue 通过axios实现get请求、post请求-----各2种方式

通过vue实现get请求、post请求-----------各2种方式

axiso文档:http://www.axios-js.com/zh-cn/docs/index.html

使用GET请求

第一种使用纯url拼接方式

get1_:function() {
                axios.get('/ajax/get?id=1&name=xcc')
                    .then(function (response) {
                        console.log(response.data.data);
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },

第二种使用传递参数的形式

get2_:function() {
                axios.get('/ajax/get',{
                    params:{
                        id:1,
                        name:'xcc'
                    }
                }).then(function (response) {
                    console.log(response.data.data);
                }).catch(function (error) {
                    console.log(error);
                });
            },

后台参数接收

@GetMapping("/get")
    public Map<String,Object> testGet(String id,String name){
        Map<String,Object> map = new HashMap<>();
        User user = new User();
        user.setId(id);
        user.setName(name);
        map.put("status",0);
        map.put("msg","请求成功");
        map.put("data",user);
        return map;
    }

使用POST请求

第一种使用Form Data形式传递

post1_:function () {
                axios.post('/ajax/post', {
                    id: '1',
                    name: 'abc'
                },{
                    transformRequest: [function (data) {
                        let params = '';
                        for(let i in data){
                            params+=i+"="+data[i]+"&";
                        }
                        return params.substring(0,params.length-1);
                    }],
                }).then(function (response) {
                     console.log(response.data.data);
                }).catch(function (error) {
                     console.log(error);
                });
            },

后台接收参数

@PostMapping("/post")
    public Map<String,Object> testPost(User user){
        Map<String,Object> map = new HashMap<>();
        map.put("status",0);
        map.put("msg","请求成功");
        map.put("data",user);
        return map;
    }

第二种使用JSON请求

post2_:function () {
                axios.post('/ajax/post2', {
                    id: '2',
                    name: 'abc'
                }).then(function (response) {
                    console.log(response.data.data);
                }).catch(function (error) {
                    console.log(error);
                });
            }

后台必须使用@RequestBody接收

@PostMapping("/post2")
    public Map<String,Object> testPost2(@RequestBody User user){
        Map<String,Object> map = new HashMap<>();
        map.put("status",0);
        map.put("msg","请求成功");
        map.put("data",user);
        return map;
    }

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值