-
//axios 實例
let instead=axios.create({
baseurl:Xxx,
timeout:1000
})
instead.get('/data.json').then(res=>{
console.log(res)
})
查看全部 -
//并發(fā)請求:同時請求多個接口數(shù)據(jù)
//axios.all--同時請求多個axios.spread ---請求后統(tǒng)一處理返回值 分割處理
axios.all([
axios.get('/data.json'),
axios.get('/city.json')
]).then(
axios.spread((data res,cityres)=>{
console.log(data res,cityres)
})
查看全部 -
get 獲取數(shù)據(jù)
post 提交數(shù)據(jù) 整個提交 如圖片 表單
put 更新數(shù)據(jù) 全部數(shù)據(jù)提交
patch 更新數(shù)據(jù) 更新修改的數(shù)據(jù)
delete 刪除數(shù)據(jù)
//post put patch 可使用formData
let formData =new form Data()
//一種方式
axios.delete('/json',{
params :{
id:12//接口請求會帶參數(shù)
}
}).then(res=>{
console.log(res)
})
//另一種方式
axios({
method:'delete',
url:'/json',
params:{},
data:{}
}).then(res=>{
console.log(res)
})
查看全部 -
axios 實例
查看全部 -
axios post請求
查看全部 -
axios post請求
查看全部 -
axios get請求
查看全部 -
axios get請求
查看全部 -
post form-data請求
查看全部 -
post請求
用?applicition/json格式攜帶數(shù)據(jù)
??
用form-data格式? (常用來表單提交 圖片上傳,文件上傳)
put和patch請求,是和post的請求方式是一樣的,請求數(shù)據(jù)格式也是兩種
delete請求
參數(shù)只有兩個
并發(fā)請求
axios.all()中接收一個數(shù)組,數(shù)組中可以發(fā)送多個請求,axios.spread()中是回調(diào)函數(shù),用來接收返回值
創(chuàng)建axios實例
當我們請求的端口號不同,超時時間不同,可以創(chuàng)建axios實例,為其單獨配置相關(guān)數(shù)據(jù)
攔截器
在請求或響應(yīng)被處理前攔截
請求攔截器
響應(yīng)攔截器
查看全部 -
---axios---
npm install axios 安裝
import axios from ‘a(chǎn)xios’?引入
請求的五種方式
Get:請求數(shù)據(jù)
Post:提交數(shù)據(jù)(表單提交)
Put:更新數(shù)據(jù)(所有數(shù)據(jù)推送給后端)
Patch:更新數(shù)據(jù)(將更改的數(shù)據(jù)推送給后端)
Delete:刪除數(shù)據(jù)
查看全部 -
封裝請求
async 方法查看全部 -
并發(fā):?
axios.all([
????{url:"",methed:""},
????{url:"",methed:""},
]).then(axios.par...)
查看全部 -
axios錯誤處理 :請求錯誤時進行的處理
axios.interceptors.request.use(config=>{
????return?config
},err=>{
????return?Promise.reject(err)?
})
axios.interceptors.response.use(res=>{
????return?res
},err=>{
????return?Promise.reject(err)??
})
//無論是請求錯誤還是響應(yīng)錯誤都會進到catch里
axios.get('/data.json').then(res=>{
}).catch(err=>{
????console.log(err)
})
//例子,實際開發(fā)中?一般添加統(tǒng)一的錯誤處理
let?instance?=?axios.create({})
instance.interceptors.request.use(config=>{
????return?config
},err=>{
????//請求錯誤?一般http狀態(tài)碼開頭,常見?401超時?404?not?fonud
????$('#modal').show()
????setTimeout(()=>{
????????$('#modal').hide()
????},2000)
????return?Promise.reject(err)??
})
instance.interceptors.response.use(res=>{
????return?res
},err=>{
????//響應(yīng)錯誤處理?一般http開頭的狀態(tài)碼以5開頭,?500系統(tǒng)錯誤??502系統(tǒng)重啟
????$('#modal').show()
????setTimeout(()=>{
????????$('#modal').hide()
????},2000)
????return?Promise.reject(err)??
})
//如果是不需要做特殊的錯誤處理就不需要寫.catch
instance.get('/data.json').then(res=>{
????console.log(res)???//
}).catch(err=>{})查看全部 -
下載老師用node寫的后臺接口項目,然后啟用Powershell窗口安裝依賴,然后輸入node index.js命令運行,啟用接口服務(wù)
查看全部
舉報