Axios传值的几种方式

2024-04-13 1906阅读
 
 
 

axios基本使用

默认是get请求

注意:get请求无请求体,可以有body,但是不建议带

使用get方式进行无参请求

     axios({
         url:'http://localhost:8080/get/getAll',
         method:'get'
     }).then(res=>{
         console.log(res.data.data)
     })
 
 @GetMapping("/get/getAll")
     public ResResult getAllUser(){
         List list = userService.list();
         return ResResult.okResult(list);
     }

 使用get方式请求,参数值直接放在路径中

 

     axios({
         url:'http://localhost:8080/get/1',
         method:'get'
     }).then(res=>{
         console.log(res.data.data)
     })
 
 后端接口
 @GetMapping("/get/{id}")
 public ResResult getUserById(@PathVariable("id") Long id){
         User user = userService.getById(id);
         return ResResult.okResult(user);
 }

 使用get方式请求,参数拼接在路径中:方式① 

     axios({
         url:'http://localhost:8080/get?id=1',
         method:'get'
     }).then(res=>{
         console.log(res.data.data)
     })
 
 后端接口
 @GetMapping("/get")
     public ResResult getUserByIds(@RequestParam("id") Long id){
         User user = userService.getById(id);
         return ResResult.okResult(user);
 }

 使用get方式请求,参数拼接在路径中:方式②

     axios({
         url:'http://localhost:8080/get',
         params:{
             id:'2'
         },
         method:'get'
     }).then(res=>{
         console.log(res.data.data)
     })
 
后端接口
@GetMapping("/get")
    public ResResult getUserByIds(@RequestParam("id") Long id){
        User user = userService.getById(id);
        return ResResult.okResult(user);
}

使用get方式请求,拼接多个参数在路径中:方式③ 

    axios({
        url:'http://localhost:8080/get',
        params:{
            id:'2',
            username:'swx'
        },
        method:'get'
    }).then(res=>{
        console.log(res.data.data)
    })

后端接口
@GetMapping("/get")
    public ResResult getUserByIds(@RequestParam("id") Long id,@RequestParam("username") String username){
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
        wrapper.eq(User::getUsername,username);
        wrapper.eq(User::getId,id);
        User user = userService.getOne(wrapper);
        return ResResult.okResult(user);
 }

 post请求接收json格式数据

    axios({
        url:'http://localhost:8080/post/test',
        data:{
            'username':'swx'
        },
        method:'post'
    }).then(res=>{
        console.log(res.data.data)
    })

后端接口
@PostMapping("/post/test")
    public ResResult getUserByIdPostTest(@RequestBody User user){
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
        wrapper.eq(User::getUsername,user.getUsername());
        User users = userService.getOne(wrapper);
        return ResResult.okResult(users);
    }

3、请求简写方式&请求失败处理 

get无参请求

    axios.get('http://localhost:8080/get/getAll').then(res=>{
        console.log(res.data.data)
    }).catch(err=>{
        console.log('timeout')
        console.log(err)
    })

get有参请求,post方式不可以这样请求

    axios.get('http://localhost:8080/get',{params:{id:'2',username:'swx'}}).then(res=>{
        console.log(res.data.data)
    }).catch(err=>{
        console.log('timeout')
        console.log(err)
    })

 post有参请求,以json格式请求

    axios.post('http://localhost:8080/post',"id=2&username=swx").then(res=>{
        console.log(res.data.data)
    }).catch(err=>{
        console.log('timeout')
        console.log(err)
    })

也可以一下方式,但是后端要加@RequestBody注解

    axios.post('http://localhost:8080/post/test',{username:'swx'}).then(res=>{
        console.log(res.data.data)
    }).catch(err=>{
        console.log('timeout')
        console.log(err)
    })

axios并发请求

    axios.all([
        axios.get('http://localhost:8080/get/getAll'),
        axios.get('http://localhost:8080/get/get',{params:{id:'1'}})
    ]).then(res=>{
        //返回的是数组,请求成功返回的数组
        console.log(res[0].data.data),
        console.log(res[1].data.data)
    }).catch(err=>{
        console.log(err)
    })

后端接口
@GetMapping("/get/getAll")
    public ResResult getAllUser(){
        List list = userService.list();
        return ResResult.okResult(list);
    }
@GetMapping("/get/get")
    public ResResult getUserByIdt(@RequestParam("id") Long id){
        User user = userService.getById(id);
        return ResResult.okResult(user);
    }

 方式2:使用spread方法处理返回的数组

    axios.all([
        axios.get('http://localhost:8080/get/getAll'),
        axios.get('http://localhost:8080/get/get',{params:{id:'1'}})
    ]).then(
        //高端一些
        axios.spread((res1,res2)=>{
            console.log(res1.data.data),
            console.log(res2.data.data)
        })
    ).catch(err=>{
        console.log(err)
    })

axios全局配置

    axios.defaults.baseURL='http://localhost:8080'; //全局配置属性
    axios.defaults.timeout=5000; //设置超时时间
    //发送请求
    axios.get('get/getAll').then(res=>{
        console.log(res.data.data)
    });
    axios.post('post/getAll').then(res=>{
        console.log(res.data.data)
    });

axios实例 

    //创建实例
    let request = axios.create({
        baseURL:'http://localhost:8080',
        timeout:5000
    });
    //使用实例
    request({
        url:'get/getAll'
    }).then(res=>{
        console.log(res.data.data)
    });
    request({
        url:'post/getAll',
        method:'post'
    }).then(res=>{
        console.log(res.data.data)
    })

Axios各种参数携带方式详解 - 知乎 (zhihu.com)

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]