国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解axios 全攻略之基本介紹與使用(GET 與 POST)

2024-05-06 15:26:40
字體:
來源:轉載
供稿:網(wǎng)友

axios

axios 是一個基于 Promise 的 HTTP 客戶端,專門為瀏覽器和 node.js 服務

Vue 2.0 官方推薦使用 axios 來代替原來的 Vue request,所以這里介紹一下 axios 的功能和基本的使用方法,希望能夠?qū)Ω魑凰袔椭?。^_^

功能

在瀏覽器中發(fā)送 XMLHttpRequests 請求 在 node.js 中發(fā)送 http 請求 支持 Promise API 攔截請求和響應 轉換請求和響應數(shù)據(jù) 取消請求 自動轉換 JSON 數(shù)據(jù)格式 客戶端支持防范 XSRF 攻擊

瀏覽器支持

axios 能夠支持 IE7 以上的 IE 版本,同時能夠支持大部分主流的瀏覽器,需要注意的是,你的瀏覽器需要支持 Promise,才能夠使用 axios。所以比較好的做法是先安裝 polyfill,然后再使用 axios。

安裝

Using npm:

$ npm install axios

Using bower:

$ bower install axios

Using cdn:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

使用

這里以 Vue 為例:在 NPM 中安裝 axios 之后,需要在 main.js 文件中引用 package

import axios from 'axios'

然后全局綁定

Vue.prototype.$http = axios

然后可以在 .vue 文件中使用 $http 來代替 axios

GET

// Make a request for a user with a given ID axios.get('/user?ID=12345') .then(function (response) {  console.log(response); }) .catch(function (error) {  console.log(error); });// Optionally the request above could also be done as axios.get('/user', {  params: {   ID: 12345  } }) .then(function (response) {  console.log(response); }) .catch(function (error) {  console.log(error); }); 

POST

axios.post('/user', {  firstName: 'Fred',  lastName: 'Flintstone' }) .then(function (response) {  console.log(response); }) .catch(function (error) {  console.log(error); });

同時發(fā)送多個請求

function getUserAccount() { return axios.get('/user/12345');}function getUserPermissions() { return axios.get('/user/12345/permissions');}axios.all([getUserAccount(), getUserPermissions()]) .then(axios.spread(function (acct, perms) {  // Both requests are now complete  }));

當然,axios 的功能還包括 axios API、interceptor 等等,這里想要詳細了解的可以查看官方文檔:axios,后面陸續(xù)會介紹下 interceptor 的使用和各類參數(shù)的配置。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 印江| 荥阳市| 福贡县| 都江堰市| 大新县| 平谷区| 云南省| 屏南县| 上栗县| 雷山县| 新龙县| 开封县| 龙岩市| 阿巴嘎旗| 山东省| 宜章县| 凤庆县| 新营市| 松原市| 金乡县| 广西| 凤山市| 汉寿县| 靖江市| 左云县| 安顺市| 新建县| 冕宁县| 闽侯县| 万盛区| 繁昌县| 新竹县| 阿拉善盟| 洪湖市| 澳门| 讷河市| 衡阳市| 昭平县| 友谊县| 蒙山县| 永新县|