網站首頁 編程語言 正文
1 axios介紹
axios 是一個基于 promise 的 HTTP 庫,可以用在瀏覽器和 node.js 中。它可以提供以下服務:
1、從瀏覽器中創建XMLHttpRequest(該對象是ajax(異步請求)的核心)
2、從node.js創建http請求
3、支持PromiseAPI
4、攔截請求和響應
5、轉換請求數據和響應數據
6、取消請求
7、自動轉換JSON數據
8、客戶端支持防御XSRF
2 使用方法
2.1 在React中安裝axios
npm install axios
2.2 get請求
1、發起不帶參數的get請求:
// 方式1 axios({methods: 'get', url: '/url'}) .then(res => { // 請求成功后的處理 // res是服務器返回的響應數據 }).catch(err => { // 請求失敗后的處理 // err是請求失敗后的信息 }) // 方式2 axios.get("url") .then(res => { // 請求成功后的處理 // res是服務器返回的響應數據 }).catch(err => { // 請求失敗后的處理 // err是請求失敗后的信息 })
2、發起帶參數的get請求:在服務器端獲取請求參數的方式 —> req.query.參數名
// 方式1 axios.get("url", {params: {參數名: 參數值}}) .then(res => { }) .catch(err => { }) // 方式2 axios({ method: "get", url: "url", params: { 參數名: 參數值 } }) .then(res => { }) .catch(err => { })
2.3 post請求:發送表單數據和文件上傳
1、發起不帶參數的post請求
// 方式1 axios({ method: "post", url: "url" }).then(res => { }).catch(err => { }) // 方式2 axios.post("url") .then(res => { }).catch(err => { })
2、發起帶參數的post請求:在服務器端獲取請求參數的方式 —> req.body.參數名
// 方式1 axios({ method: "post", url: "url", data: { 參數名: 參數值 } }).then(res => { }).catch(err => { }) // 方式2 axios.post("url", {參數名: 參數值}) .then(res => { }).catch(err => { })
2.4 put請求:對數據進行全部更新
1、發起不帶參數的put請求
// 方式1 axios({ method: "put", url: "url" }).then(res => { }).catch(err => { }) // 方式2 axios.put("url") .then(res => { }).catch(err => { })
2、發起帶參數的put請求:在服務器端獲取請求參數的方式 —> req.body.參數名
// 方式1 axios({ method: "put", url: "url", data: { 參數名: 參數值 } }).then(res => { }).catch(err => { }) // 方式2 axios.put("url", {參數名: 參數值}) .then(res => { }).catch(err => { })
2.5 patch請求:只對更改過的數據進行更新
1、發起不帶參數的patch請求
// 方式1 axios({ method: "patch", url: "url" }).then(res => { }).catch(err => { }) // 方式2 axios.patch("url") .then(res => { }).catch(err => { })
2、發起帶參數的patch請求:在服務器端獲取請求參數的方式 —> req.body.參數名
// 方式1 axios({ method: "patch", url: "url", data: { 參數名: 參數值 } }).then(res => { }).catch(err => { }) // 方式2 axios.patch("url", {參數名: 參數值}) .then(res => { }).catch(err => { })
2.6 delete請求:刪除請求(參數可以放在url上,也可以和post一樣放在請求體中)
1、可以像get請求一樣包裝請求參數:在服務器端獲取請求參數的方式 —> req.query.參數名
axios.delete('url', { params: { 參數名: 參數值 } }).then(res => { }).catch(err => { })
2、可以像post請求一樣包裝請求參數:在服務器端獲取請求參數的方式 —> req.body.參數名
axios.delete('url', {data: {參數名: 參數值}}) .then(res => { }) .catch(err => { })
3 axios的響應結構
{ // `data` 由服務器提供的響應 data: {}, // `status` HTTP 狀態碼 status: 200, // `statusText` 來自服務器響應的 HTTP 狀態信息 statusText: "OK", // `headers` 服務器響應的頭 headers: {}, // `config` 是為請求提供的配置信息 config: {} }
后臺:res.json(result)
,發送了json格式的數據,相當于:{ data: result }
前端:res.data
例如后臺:
res.json({ code: 1001, msg: '橘貓吃不胖' })
前端:
res.data.code // 1001 res.data.msg // 橘貓吃不胖
原文鏈接:https://blog.csdn.net/m0_46612221/article/details/123341393
相關推薦
- 2023-05-31 如何在ubuntu中切換使用不同版本的python_python
- 2022-09-09 pycharm中創建sql文件及模板的過程_python
- 2022-08-01 Python+Selenium鍵盤鼠標模擬事件操作詳解_python
- 2023-02-12 python語音信號處理詳細教程_python
- 2022-11-06 Android?Navigation重建Fragment問題分析及解決_Android
- 2023-10-28 C++?string和wstring相互轉換方式_C 語言
- 2022-03-25 C語言設計模式之命令模式介紹_C 語言
- 2022-05-18 Python中的turtle畫箭頭,矩形,五角星_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支