国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Axios傳值的幾種方式

這篇具有很好參考價(jià)值的文章主要介紹了Axios傳值的幾種方式。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

 <body>
 <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
 </body>

axios基本使用

默認(rèn)是get請求

注意:get請求無請求體,可以有body,但是不建議帶

使用get方式進(jìn)行無參請求

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

?使用get方式請求,參數(shù)值直接放在路徑中

?

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

?使用get方式請求,參數(shù)拼接在路徑中:方式①?

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

?使用get方式請求,參數(shù)拼接在路徑中:方式②

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

使用get方式請求,拼接多個(gè)參數(shù)在路徑中:方式③?

<script>
    axios({
        url:'http://localhost:8080/get',
        params:{
            id:'2',
            username:'swx'
        },
        method:'get'
    }).then(res=>{
        console.log(res.data.data)
    })
</script>
后端接口
@GetMapping("/get")
    public ResResult getUserByIds(@RequestParam("id") Long id,@RequestParam("username") String username){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);
        wrapper.eq(User::getId,id);
        User user = userService.getOne(wrapper);
        return ResResult.okResult(user);
 }

?post請求接收json格式數(shù)據(jù)

<script>
    axios({
        url:'http://localhost:8080/post/test',
        data:{
            'username':'swx'
        },
        method:'post'
    }).then(res=>{
        console.log(res.data.data)
    })
</script>
后端接口
@PostMapping("/post/test")
    public ResResult getUserByIdPostTest(@RequestBody User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        User users = userService.getOne(wrapper);
        return ResResult.okResult(users);
    }

3、請求簡寫方式&請求失敗處理?

get無參請求

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

get有參請求,post方式不可以這樣請求

<script>
    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)
    })
</script>

?post有參請求,以json格式請求

<script>
    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)
    })
</script>


也可以一下方式,但是后端要加@RequestBody注解
<script>
    axios.post('http://localhost:8080/post/test',{username:'swx'}).then(res=>{
        console.log(res.data.data)
    }).catch(err=>{
        console.log('timeout')
        console.log(err)
    })
</script>

axios并發(fā)請求

<script>
    axios.all([
        axios.get('http://localhost:8080/get/getAll'),
        axios.get('http://localhost:8080/get/get',{params:{id:'1'}})
    ]).then(res=>{
        //返回的是數(shù)組,請求成功返回的數(shù)組
        console.log(res[0].data.data),
        console.log(res[1].data.data)
    }).catch(err=>{
        console.log(err)
    })
</script>
后端接口
@GetMapping("/get/getAll")
    public ResResult getAllUser(){
        List<User> 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方法處理返回的數(shù)組

<script>
    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)
    })
</script>

axios全局配置

<script>
    axios.defaults.baseURL='http://localhost:8080'; //全局配置屬性
    axios.defaults.timeout=5000; //設(shè)置超時(shí)時(shí)間

    //發(fā)送請求
    axios.get('get/getAll').then(res=>{
        console.log(res.data.data)
    });

    axios.post('post/getAll').then(res=>{
        console.log(res.data.data)
    });
</script>

axios實(shí)例?

<script>
    //創(chuàng)建實(shí)例
    let request = axios.create({
        baseURL:'http://localhost:8080',
        timeout:5000
    });
    //使用實(shí)例
    request({
        url:'get/getAll'
    }).then(res=>{
        console.log(res.data.data)
    });

    request({
        url:'post/getAll',
        method:'post'
    }).then(res=>{
        console.log(res.data.data)
    })
</script>

Axios各種參數(shù)攜帶方式詳解 - 知乎 (zhihu.com)文章來源地址http://www.zghlxwxcb.cn/news/detail-854677.html

到了這里,關(guān)于Axios傳值的幾種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 微信小程序的幾種傳值方式

    微信小程序的幾種傳值方式

    目錄 一、使用全局變量傳遞數(shù)據(jù) 二、本地存儲(chǔ)傳遞數(shù)據(jù) 三、使用路由傳遞數(shù)據(jù) 四、父子組件之間傳值 提示:利用 app.js 中的 globalData 將數(shù)據(jù)存儲(chǔ)為 全局變量 ,在需要使用的頁面通過 getApp().globalData 獲取 提示:利用微信小程序提供的本地存儲(chǔ) wx.setStorageSync ?與 wx.getStorage

    2023年04月09日
    瀏覽(18)
  • 【Vue3】路由傳參的幾種方式

    【Vue3】路由傳參的幾種方式

    路由導(dǎo)航有兩種方式,分別是:聲明式導(dǎo)航 和 編程式導(dǎo)航 參數(shù)分為query參數(shù)和params參數(shù)兩種 1.傳參 在路由路徑后直接拼接 ?參數(shù)名:參數(shù)值 ,多組參數(shù)間使用 分隔。 如果參數(shù)值為變量,需要使用模版字符串。 2.接收與使用 1.傳參 to不再傳遞字符,而是傳一個(gè)對象,由于參數(shù)

    2024年02月21日
    瀏覽(26)
  • taro跳轉(zhuǎn)頁面?zhèn)鲄⒌膸追N方式

    我之前在網(wǎng)上也搜了挺多taro傳參的方式,這里我總結(jié)一下 路由跳轉(zhuǎn)分Taro.navigateTo與Taro.redirectTo, 但是這兩種方法只適用于傳遞少量參數(shù) Taro.navigateTo跳轉(zhuǎn)時(shí)是將新的頁面加載過來,最多加載到10層,返回時(shí)去的是上一頁; Taro.redirectTo跳轉(zhuǎn)的同時(shí)將當(dāng)前頁面銷毀,返回時(shí)去的是

    2024年02月07日
    瀏覽(45)
  • 微信小程序頁面之間傳參的幾種方式

    目錄 前言 第一種:url傳值 url傳值使用詳細(xì)說明 api跳轉(zhuǎn) 組件跳轉(zhuǎn) 第二種:將值緩存在本地,再從本地取值 第三種:全局傳值(應(yīng)用實(shí)例傳值) 第四種:組件傳值 第五種:使用通信通道(通信通道是wx.navitageTo()獨(dú)有的) 第六中:使用頁面棧(只對當(dāng)前頁面棧中存在的頁面生效

    2024年04月13日
    瀏覽(31)
  • vue父子組件之間的傳參的幾種方式

    這是最常用的一種方式。通過props選項(xiàng),在父組件中傳遞數(shù)據(jù)給子組件。在子組件中使用props聲明該屬性,就可以訪問到父組件傳遞過來的數(shù)據(jù)了。 子組件向父組件傳遞數(shù)據(jù)的方式。在子組件中使用emit方法觸發(fā)一個(gè)自定義事件,并通過參數(shù)傳遞數(shù)據(jù)。在父組件中監(jiān)聽這個(gè)事件

    2023年04月24日
    瀏覽(101)
  • C#面:列舉ASP.NET頁面之間傳遞值的幾種方式

    查詢字符串(Query String): 可以通過在URL中添加參數(shù)來傳遞值。 例如:http://example.com/page.aspx?id=123 在接收頁面中可以通過Request.QueryString[“id”]來獲取傳遞的值。 會(huì)話狀態(tài)(Session State): 可以使用Session對象在不同頁面之間存儲(chǔ)和檢索值。 在發(fā)送頁面中可以使用Session[“k

    2024年02月19日
    瀏覽(19)
  • 原生js創(chuàng)建get/post請求以及封裝方式、axios的基本使用

    原生js創(chuàng)建get請求 原生js創(chuàng)建post請求 原生get和post封裝方式1 原生get和post封裝方式2 axios的基本使用

    2024年02月21日
    瀏覽(24)
  • axios (用法、傳參等)

    axios (用法、傳參等)

    是一個(gè)專注于網(wǎng)絡(luò)請求的庫。 中文官網(wǎng)地址: http://www.axios-js.com/ 可直接點(diǎn)擊這里跳到中文官網(wǎng) 英文官網(wǎng)地址: https://www.npmjs.com/package/axios 可直接點(diǎn)擊這里跳轉(zhuǎn)到英文官網(wǎng) 直接引入 然后在全局下就有這個(gè)方法了 結(jié)果: 結(jié)論: 調(diào)用 axios 方法得到的返回值是 Promise 對象 然后

    2024年02月09日
    瀏覽(12)
  • 詳解axios四種傳參,后端接參

    詳解axios四種傳參,后端接參

    前端瀏覽器發(fā)送的數(shù)據(jù) 后端接參 用 @RequestBody 指定接收的是 json 格式的參數(shù),然后參數(shù)類型是 Map類型 ,通過map的鍵取出數(shù)據(jù)。 后端服務(wù)器接收的數(shù)據(jù):{aid=11} 前端瀏覽器發(fā)送的數(shù)據(jù) 后端接收: 用 @RequestBody 指定接收的是 json 格式的參數(shù),然后參數(shù)可以 通過名字自動(dòng)匹配

    2024年02月11日
    瀏覽(21)
  • 微信小程序頁面?zhèn)髦档?種方式

    微信小程序頁面?zhèn)髦档姆绞接幸韵聨追N: 1.URL參數(shù)傳值:通過在跳轉(zhuǎn)鏈接中附加參數(shù),在目標(biāo)頁面的onLoad函數(shù)中獲取參數(shù)。 2.全局變量:通過在app.js文件中定義全局變量,在源頁面設(shè)置變量的值,目標(biāo)頁面通過getApp().globalData獲取變量的值。 3.緩存存儲(chǔ):使用wx.setStorageSync()在

    2024年02月15日
    瀏覽(17)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包