vue axios
vue2.0之后,就不再对vue-resource更新,而是推荐使用axios。基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 Node.js 中使用。
功能特性
1、在浏览器中发送 XMLHttpRequests 请求
2、在 node.js 中发送 http请求
3、支持 Promise API
4、拦截请求和响应
5、转换请求和响应数据
6、取消请求
7、自动转换 JSON 数据
8、客户端支持保护安全免受 CSRF/XSRF 攻击
安装 axios
$ npm install axios
在要使用的文件中引入axios
import axios from 'axios'
GET请求
// 向具有指定ID的用户发出请求axios.get('/user?ID=12345').then(function(response){console.log(response);}).catch(function(error){console.log(error);});// 也可以通过 params 对象传递参数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);});