axios基本的get/post請求使用方法


注冊

如果在Vue中使用,npm安裝后,通過

import Vue from 'vue'
import axios from 'axios'
Vue.prototype.axios = axios

掛載到Vue實例上

使用(兩種方法):

this.axios.get(url).then(response => { // axios的get/post方法本身就是一個Promise對象
      console.log(response)
      }).catch((error) => {
      console.log(error)
      }
)
async function(){
      let response  =  await this.axios.post(url,data); // response是一個response對象,已經被封裝好了
      console.log(reponse);  
      console.log(response.data, response.status); // 注意status如果是404,則會在await時候報錯!!!根本得不到response
}

結果返回:

{
data:xxxx,
status:200,
headers:{xxx},
request:XMLHttpRequest {xxx},
statusTEXT:'ok',
__proto__: Object
}

由此可以根據返回對象判斷結果,但是這並不能表明:對於Promise對象,可以用await獲取內部的值


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM