電影里的做視頻在線觀看網(wǎng)站無(wú)貨源網(wǎng)店怎么開(kāi)
1. axios的基本特性
axios 是一個(gè)基于Promise用于瀏覽器和node.js的HTTP客戶端。
它具有以下特征:
- 支持瀏覽器和node.js
- 支持promiseAPI
- 自動(dòng)轉(zhuǎn)換JSON數(shù)據(jù)
- 能攔截請(qǐng)求和響應(yīng)請(qǐng)求
- 轉(zhuǎn)換請(qǐng)求數(shù)據(jù)和響應(yīng)數(shù)據(jù)(請(qǐng)求是可以加密,在返回時(shí)也可進(jìn)行解密)
2. axios的基本用法
//客戶端請(qǐng)求
axios.get('http://localhost:3000/adata').then(ret =>{//data屬性名稱是固定的,用于獲取后臺(tái)響應(yīng)的數(shù)據(jù)console.log(ret.data)})
//服務(wù)器端響應(yīng)
app.get('/adata', (req, res) => {res.send('Hello axios!')
})
- 服務(wù)器端響應(yīng)的是ret對(duì)象
- data屬性是我們需要的數(shù)據(jù),獲取方法:ret.data(對(duì)象.屬性名)
3. axios的常用API
- get:查詢數(shù)據(jù)
- post:添加數(shù)據(jù)
- put:修改數(shù)據(jù)
- delete:刪除數(shù)據(jù)
4. axios的參數(shù)傳遞🔥
4.1 get傳遞參數(shù)
第一種方式
-
通過(guò)URL傳遞參數(shù)
- 1. 傳統(tǒng)url地址 通過(guò)?傳參
//客戶端請(qǐng)求 <body><script type="text/javascript" src="js/axios.js"></script><script type="text/javascript">//axios get傳統(tǒng)url地址請(qǐng)求傳參axios.get('http://localhost:3000/axios?id=123').then(function (ret) {console.log(ret.data)})</script> </body> //服務(wù)器響應(yīng) app.get('/axios', (req, res) => {res.send('axios get 傳遞參數(shù)' + req.query.id) })
- 2. 通過(guò)restful形式的url(用params接收參數(shù))
//客戶端請(qǐng)求
<body><script type="text/javascript" src="js/axios.js"></script><script type="text/javascript">//axios getrestful形式的url請(qǐng)求傳參axios.get('http://localhost:3000/axios/456').then(function(ret){console.log(ret.data)})</script>
</body>
//服務(wù)器響應(yīng)
app.get('/axios/:id', (req, res) => {res.send('axios get (Restful) 傳遞參數(shù)' + req.params.id)
})
第二種方式
- 通過(guò)params選項(xiàng)傳遞參數(shù)(比較方便,傳遞多個(gè)參數(shù)的 時(shí)候)
//客戶端請(qǐng)求
<body><script type="text/javascript" src="js/axios.js"></script><script type="text/javascript">//axios get通過(guò)params選項(xiàng)傳遞參數(shù)axios.get('http://localhost:3000/axios', {params: {id: 789}}).then(function (ret) {console.log(ret.data)})</script>
</body>
//服務(wù)器響應(yīng)
app.get('/axios', (req, res) => {res.send('axios get 傳遞參數(shù)' + req.query.id)
})
4.2 delete傳遞參數(shù)
參數(shù)傳遞方式和get相似(兩種)
- 通過(guò)url地址傳參
- 傳統(tǒng)url地址 通過(guò)?傳參
- restful形式的url(用params接收參數(shù))
- 通過(guò)params(用query接收參數(shù))
4.3 post傳遞參數(shù)
第一種方式
- 通過(guò)選項(xiàng)傳遞參數(shù)(默認(rèn)傳遞的是json格式的數(shù)據(jù))
//客戶端請(qǐng)求
<body><script type="text/javascript" src="js/axios.js"></script><script type="text/javascript">//axios post傳遞參數(shù)axios.post('http://localhost:3000/axios', {uname: 'xuhui那束光',pwd: 123}).then(function (ret) {console.log(ret.data)})</script>
</body>
//服務(wù)器響應(yīng)
app.post('/axios', (req, res) => {res.send('axios post 傳遞參數(shù)' + req.body.uname + '---' + req.body.pwd)
})
- 提交的數(shù)據(jù)格式是JSON形式,需要服務(wù)器端提供JSON支持🔥
//服務(wù)器端支持
app.use(bodyParser.json());
第二種方式
- 通過(guò)URLsearchParams傳遞參數(shù)(application/x-www-for,-urlencoded)
//客戶端請(qǐng)求
<body><script type="text/javascript" src="js/axios.js"></script><script type="text/javascript">//axios post傳遞參數(shù)var params = new URLSearchParams();params.append('uname', 'xuhui那束光');params.append('pwd', '5555');axios.post('http://localhost:3000/axios', params).then(function(ret){console.log(ret.data)})</script>
</body>
//服務(wù)器響應(yīng)
app.post('/axios/:id', (req, res) => {res.send('axios put 傳遞參數(shù)' + req.params.id + '---' + req.body.uname + '---' + req.body.pwd)
})
- 提交的數(shù)據(jù)格式為字符串形式
4.4 put傳遞參數(shù)
參數(shù)傳遞方式與post相似(選項(xiàng)傳參和URLsearchParams傳參)
//客戶端請(qǐng)求
<body><script type="text/javascript" src="js/axios.js"></script><script type="text/javascript">//axios put 請(qǐng)求傳參axios.put('http://localhost:3000/axios/123', {uname: 'xuhui那束光',pwd: 123}).then(function (ret) {console.log(ret.data)})</script>
</body>
//服務(wù)器響應(yīng)
app.put('/axios/:id', (req, res) => {res.send('axios put 傳遞參數(shù)' + req.params.id + '---' + req.body.uname + '---' + req.body.pwd)
})
5.axios的響應(yīng)結(jié)果
響應(yīng)結(jié)果的主要屬性:
- data:實(shí)際響應(yīng)回來(lái)的數(shù)據(jù)
- headers:響應(yīng)頭信息
- status:響應(yīng)狀態(tài)碼
- statusText:響應(yīng)狀態(tài)信息
axios.get('http://localhost:3000/axios').then(function (ret) {console.log(ret)
})
- data絕大多數(shù)場(chǎng)景返回來(lái)的是JSON形式的數(shù)據(jù)🔥
//向服務(wù)器請(qǐng)求JSON接口
axios.get('http://localhost:3000/axios-json').then(function (ret) {console.log(ret.data.uname)
})
//服務(wù)器端準(zhǔn)備一個(gè)JSON接口
app.get('/axios-json', (req, res) => {res.json({uname: 'xuhui',age: 12});
})
- data是大對(duì)象ret里面的小對(duì)象🔥
通過(guò) 對(duì)象.屬性名(data.uname) 可以獲取對(duì)應(yīng)的值
6. axios的全局配置
在發(fā)送請(qǐng)求前,可以做一些配置信息
- axios.defaults.timeout = 3000;//響應(yīng)超時(shí)時(shí)間
- axios.defaults.baseURL = 'http://localhost:3000/app';//默認(rèn)地址
- axios.defaults.headers[ ' mytoken' ] = 'aqwerarwrqrwqr' //設(shè)置請(qǐng)求頭
1. 默認(rèn)地址演示🔥
// 配置請(qǐng)求的基準(zhǔn)URL地址
axios.defaults.baseURL = 'http://localhost:3000/';
//向服務(wù)器請(qǐng)求JSON接口
axios.get('axios-json').then(function (ret) {console.log(ret.data.uname)
})//服務(wù)器端準(zhǔn)備一個(gè)JSON接口
app.get('/axios-json', (req, res) => {res.json({uname: 'xuhui',age: 12});
})
2. 設(shè)置請(qǐng)求頭
// 配置請(qǐng)求的基準(zhǔn)URL地址
axios.defaults.baseURL = 'http://localhost:3000/';
// 配置請(qǐng)求頭信息
axios.defaults.headers['mytoken'] = 'hello';
//向服務(wù)器請(qǐng)求JSON接口
axios.get('axios-json').then(function (ret) {console.log(ret.data.uname)
})//服務(wù)器端準(zhǔn)備一個(gè)JSON接口
app.get('/axios-json', (req, res) => {res.json({uname: 'xuhui',age: 12});
})
- 對(duì)于跨域請(qǐng)求來(lái)說(shuō),請(qǐng)求頭是需要后臺(tái)進(jìn)行配置的
7. axios攔截器
1.請(qǐng)求攔截器🔥
- 在請(qǐng)求發(fā)出之前設(shè)置一些信息
//axios請(qǐng)求攔截器
axios.interceptors.request.use(function(config) {console.log(config.url)config.headers.mytoken = 'nihao';return config;
}, function(err){console.log(err)
})
//向服務(wù)器發(fā)起請(qǐng)求
axios.get('http://localhost:3000/adata').then(function(data){console.log(data)
})
2.響應(yīng)攔截器🔥
- 在獲取數(shù)據(jù)之前對(duì)數(shù)據(jù)做一些加工處理
//axios響應(yīng)攔截器
axios.interceptors.response.use(function(res) {console.log(res)return res;
}, function(err){console.log(err)
})
//向服務(wù)器發(fā)起請(qǐng)求
axios.get('http://localhost:3000/adata').then(function (data) {console.log(data)
})
- (25行攔截器打印的信息 res)和 (31行最終需要的數(shù)據(jù) ) 打印的信息是完全一樣的。
- 但是,響應(yīng)攔截器res中拿到的不是具體數(shù)據(jù)
- 在調(diào)用接口時(shí),只關(guān)心實(shí)際的數(shù)據(jù),不需要包裝數(shù)據(jù)的對(duì)象,可以在設(shè)置攔截器內(nèi)容,對(duì)接收到的數(shù)據(jù)進(jìn)行處理加工🔥
- 最后拿到的data是經(jīng)過(guò)響應(yīng)攔截器處理后的數(shù)據(jù)
- 注:文中部分內(nèi)容來(lái)源于網(wǎng)絡(luò),聯(lián)系侵刪