React中的axios模塊及使用方法
1 axios介紹
axios 是一個基于 promise 的 HTTP 庫,可以用在瀏覽器和 node.js 中。它可以提供以下服務:
1、從瀏覽器中創(chuàng)建XMLHttpRequest(該對象是ajax(異步請求)的核心)
2、從node.js創(chuàng)建http請求
3、支持PromiseAPI
4、攔截請求和響應
5、轉換請求數(shù)據(jù)和響應數(shù)據(jù)
6、取消請求
7、自動轉換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是服務器返回的響應數(shù)據(jù) }).catch(err => { // 請求失敗后的處理 // err是請求失敗后的信息 }) // 方式2 axios.get("url") .then(res => { // 請求成功后的處理 // res是服務器返回的響應數(shù)據(jù) }).catch(err => { // 請求失敗后的處理 // err是請求失敗后的信息 })
2、發(fā)起帶參數(shù)的get請求:在服務器端獲取請求參數(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請求:在服務器端獲取請求參數(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ù)進行全部更新
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請求:在服務器端獲取請求參數(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ù)進行更新
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請求:在服務器端獲取請求參數(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ù):在服務器端獲取請求參數(shù)的方式 —> req.query.參數(shù)名
axios.delete('url', { params: { 參數(shù)名: 參數(shù)值 } }).then(res => { }).catch(err => { })
2、可以像post請求一樣包裝請求參數(shù):在服務器端獲取請求參數(shù)的方式 —> req.body.參數(shù)名
axios.delete('url', {data: {參數(shù)名: 參數(shù)值}}) .then(res => { }) .catch(err => { })
3 axios的響應結構
{ // `data` 由服務器提供的響應 data: {}, // `status` HTTP 狀態(tài)碼 status: 200, // `statusText` 來自服務器響應的 HTTP 狀態(tài)信息 statusText: "OK", // `headers` 服務器響應的頭 headers: {}, // `config` 是為請求提供的配置信息 config: {} }
后臺:res.json(result)
,發(fā)送了json格式的數(shù)據(jù),相當于:{ data: result }
前端:res.data
例如后臺:
res.json({ code: 1001, msg: '橘貓吃不胖' })
前端:
res.data.code // 1001 res.data.msg // 橘貓吃不胖
到此這篇關于React中的axios模塊的文章就介紹到這了,更多相關React axios模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
React Native開發(fā)封裝Toast與加載Loading組件示例
這篇文章主要介紹了React Native開發(fā)封裝Toast與加載Loading組件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09React?hook實現(xiàn)簡單的websocket封裝方式
這篇文章主要介紹了React?hook實現(xiàn)簡單的websocket封裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09詳解如何使用React和MUI創(chuàng)建多選Checkbox樹組件
這篇文章主要為大家詳細介紹了如何使用 React 和 MUI(Material-UI)庫來創(chuàng)建一個多選 Checkbox 樹組件,該組件可以用于展示樹形結構的數(shù)據(jù),并允許用戶選擇多個節(jié)點,感興趣的可以了解下2024-01-01基于React的狀態(tài)管理實現(xiàn)一個簡單的顏色轉換器
這篇文章主要介紹了用React的狀態(tài)管理,簡簡單單實現(xiàn)一個顏色轉換器,文中有詳細的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-08-08Yarn安裝項目依賴報error?An?unexpected?error?occurred:?“XXXXX:E
這篇文章主要為大家介紹了Yarn安裝項目依賴報error?An?unexpected?error?occurred:?“XXXXX:ESOCKETTIMEOUT”問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03