axios简单应用 和 fetch的简单应用

首先要明白的是axios是什么:axios是基于promise(诺言)用于浏览器和node.js是http客户端。

axios的作用是什么呢:axios主要是用于向后台发起请求的,还有在请求中做更多是可控功能。

安装axios

  小黑窗口 执行命令 npm i axios --save

在需要数据请求的页面导入 axios

  import axios from "axios"

案例

执行 GET 请求

// 为给定 ID 的 user 创建请求
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

// 上面的请求也可以这样做
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);
});

**************************************************************************************************************************************

原生js接口数据获取方法 fetch

注:不支持低版本浏览器使用

语法

  fetch("url地址端口").then((res)=>{

    return res.json();

  }).then((res)=>{

    console.log(res)//此处写入想要如何处理函数,一般存入自己的数据中,this.list=res

  })

猜你喜欢

转载自www.cnblogs.com/lifeidg/p/10884872.html
今日推荐