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获取内部的值

posted @ 2020-09-10 21:02  Bravo_Jack  阅读(2838)  评论(0编辑  收藏  举报