日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

axios 的简单使用

發布時間:2025/3/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 axios 的简单使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Vue

axios

  • Promise based HTTP client for the browser and node.js
    • 以Promise為基礎的HTTP客戶端,適用于:瀏覽器和node.js
    • 類似于ajax,用來發送請求,異步獲取數據
  • 安裝:npm i -S axios
導入 vue 1 導入 axios 2 Vue.prototype.$http = axiosthis.$http 方式來使用 axios 3 全局公共域名配置axios.defaults.baseURL = 'https://api.example.com' 4 HTTP請求的攔截器axios.interceptors.requestaxios.interceptors.response5 注意:發送post請求的時候,參數需要額外的處理 import Vue from 'vue' import axios from 'axios' // 將 axios 添加到 Vue.prototype 中 Vue.prototype.$axios = axios--- // 在組件中使用: methods: {getData() {this.$axios.get('url').then(res => {}).catch(err => {})} }--- // API使用方式:axios.get(url[, config]) axios.post(url[, data[, config]]) axios(url[, config]) axios(config)

Get 請求

// url中帶有query參數 axios.get('/user?id=89').then(function (response) {console.log(response);}).catch(function (error) {console.log(error);});// url和參數分離,使用對象 axios.get('/user', {params: {id: 12345} })

Post 請求

  • 不同環境中處理 POST請求
  • 默認情況下,axios 會將JS對象序列化為JSON對象。為了使用 application/x-www-form-urlencoded 格式發送請求,我們可以這樣:
// 使用 qs 包,處理將對象序列化為字符串 var qs = require('qs') axios.post('/foo', qs.stringify({ 'bar': 123 }))// 或者: axios.post('/foo', 'bar=123') axios.post('/user', qs.stringify({firstName: 'Fred',lastName: 'Flintstone'})).then(function (response) {console.log(response);}).catch(function (error) {console.log(error);});

axios API

可以通過傳遞相關配置來進行請求axios。

// Send a POST request axios({method: 'post',url: '/user/12345',data: {firstName: 'Fred',lastName: 'Flintstone'} });

全局配置

?```js // 設置請求公共路徑: axios.defaults.baseURL = 'https://api.example.com'

### 攔截器 ?```js // 請求攔截器 axios.interceptors.request.use(function (config) {// 所有請求之前都要執行的操作return config;}, function (error) {// 錯誤處理return Promise.reject(error);});// 響應攔截器 axios.interceptors.response.use(function (response) {// 所有請求完成后都要執行的操作return response;}, function (error) {// 錯誤處理return Promise.reject(error);});

官方鏈接:https://github.com/axios/axios

轉載于:https://my.oschina.net/shuaihong/blog/1558205

總結

以上是生活随笔為你收集整理的axios 的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。