亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

React中的axios模塊及使用方法

 更新時間:2022年03月09日 10:40:20   作者:橘貓吃不胖~  
axios 是一個基于 promise 的 HTTP 庫,可以用在瀏覽器和 node.js 中,本文給大家分享React中axios模塊的使用方法,感興趣的朋友一起看看吧

1 axios介紹

axios 是一個基于 promise 的 HTTP 庫,可以用在瀏覽器和 node.js 中。它可以提供以下服務(wù):
1、從瀏覽器中創(chuàng)建XMLHttpRequest(該對象是ajax(異步請求)的核心)
2、從node.js創(chuàng)建http請求
3、支持PromiseAPI
4、攔截請求和響應(yīng)
5、轉(zhuǎn)換請求數(shù)據(jù)和響應(yīng)數(shù)據(jù)
6、取消請求
7、自動轉(zhuǎn)換JSON數(shù)據(jù)
8、客戶端支持防御XSRF

2 使用方法

2.1 在React中安裝axios

npm install axios

2.2 get請求

1、發(fā)起不帶參數(shù)的get請求:

// 方式1
axios({methods: 'get', url: '/url'})
    .then(res => { // 請求成功后的處理
        // res是服務(wù)器返回的響應(yīng)數(shù)據(jù)
    }).catch(err => { // 請求失敗后的處理
    // err是請求失敗后的信息
})
// 方式2
axios.get("url")
    .then(res => { // 請求成功后的處理
        // res是服務(wù)器返回的響應(yīng)數(shù)據(jù)
    }).catch(err => { // 請求失敗后的處理
    // err是請求失敗后的信息
})

2、發(fā)起帶參數(shù)的get請求:在服務(wù)器端獲取請求參數(shù)的方式 —> req.query.參數(shù)名

// 方式1
axios.get("url", {params: {參數(shù)名: 參數(shù)值}})
    .then(res => {
    })
    .catch(err => {
    })

// 方式2
axios({
    method: "get",
    url: "url",
    params: {
        參數(shù)名: 參數(shù)值
    }
})
    .then(res => {
    })
    .catch(err => {
    })

2.3 post請求:發(fā)送表單數(shù)據(jù)和文件上傳

1、發(fā)起不帶參數(shù)的post請求

// 方式1
axios({
    method: "post",
    url: "url"
}).then(res => {

}).catch(err => {
    
})

// 方式2
axios.post("url")
    .then(res => {

    }).catch(err => {
    
})

2、發(fā)起帶參數(shù)的post請求:在服務(wù)器端獲取請求參數(shù)的方式 —> req.body.參數(shù)名

// 方式1
axios({
    method: "post",
    url: "url",
    data: {
        參數(shù)名: 參數(shù)值
    }
}).then(res => {

}).catch(err => {
    
})

// 方式2
axios.post("url", {參數(shù)名: 參數(shù)值})
    .then(res => {

    }).catch(err => {

})

2.4 put請求:對數(shù)據(jù)進(jìn)行全部更新

1、發(fā)起不帶參數(shù)的put請求

// 方式1
axios({
    method: "put",
    url: "url"
}).then(res => {

}).catch(err => {
    
})

// 方式2
axios.put("url")
    .then(res => {

    }).catch(err => {
    
})

2、發(fā)起帶參數(shù)的put請求:在服務(wù)器端獲取請求參數(shù)的方式 —> req.body.參數(shù)名

// 方式1
axios({
    method: "put",
    url: "url",
    data: {
        參數(shù)名: 參數(shù)值
    }
}).then(res => {

}).catch(err => {
    
})

// 方式2
axios.put("url", {參數(shù)名: 參數(shù)值})
    .then(res => {

    }).catch(err => {

})

2.5 patch請求:只對更改過的數(shù)據(jù)進(jìn)行更新

1、發(fā)起不帶參數(shù)的patch請求

// 方式1
axios({
    method: "patch",
    url: "url"
}).then(res => {

}).catch(err => {
    
})

// 方式2
axios.patch("url")
    .then(res => {

    }).catch(err => {
    
})

2、發(fā)起帶參數(shù)的patch請求:在服務(wù)器端獲取請求參數(shù)的方式 —> req.body.參數(shù)名

// 方式1
axios({
    method: "patch",
    url: "url",
    data: {
        參數(shù)名: 參數(shù)值
    }
}).then(res => {

}).catch(err => {
    
})

// 方式2
axios.patch("url", {參數(shù)名: 參數(shù)值})
    .then(res => {

    }).catch(err => {

})

2.6 delete請求:刪除請求(參數(shù)可以放在url上,也可以和post一樣放在請求體中)

1、可以像get請求一樣包裝請求參數(shù):在服務(wù)器端獲取請求參數(shù)的方式 —> req.query.參數(shù)名

axios.delete('url', {
    params: {
        參數(shù)名: 參數(shù)值
    }
}).then(res => {
}).catch(err => {
})

2、可以像post請求一樣包裝請求參數(shù):在服務(wù)器端獲取請求參數(shù)的方式 —> req.body.參數(shù)名

axios.delete('url', {data: {參數(shù)名: 參數(shù)值}})
    .then(res => {
    })
    .catch(err => {
    })

3 axios的響應(yīng)結(jié)構(gòu)

{
    // `data` 由服務(wù)器提供的響應(yīng)
    data: {},
    // `status`  HTTP 狀態(tài)碼
    status: 200,
    // `statusText` 來自服務(wù)器響應(yīng)的 HTTP 狀態(tài)信息
    statusText: "OK",
    // `headers` 服務(wù)器響應(yīng)的頭
    headers: {},
    // `config` 是為請求提供的配置信息
    config: {}
}

后臺:res.json(result),發(fā)送了json格式的數(shù)據(jù),相當(dāng)于:{ data: result }
前端:res.data

例如后臺:

res.json({
    code: 1001,
    msg: '橘貓吃不胖'
})

前端:

res.data.code // 1001
res.data.msg // 橘貓吃不胖

到此這篇關(guān)于React中的axios模塊的文章就介紹到這了,更多相關(guān)React axios模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論