axios是一个基于promise的HTTP库,可以用在浏览器和node.js中
传递请求的方法
基本方法
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
可以执行多个并发的请求
function getUserAccount() {
return axios.get('/user/12345');
}
function getUserPermissions() {
return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// 两个请求现在都执行完成
}));
区别,当使用post请求的时候,进行数据的传参使用的是data方法,而使用get请求的时候,使用的是params方法。
axiosAPI
也就是可以通过向axios传递相关配置来创建请求
// 发送 POST 请求
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
请求方式的别名:
axios
.request(
config);
axios
.get(
url
[,config]);
axios
.delete(
url
[,config]);
axios
.head(
url
[,config]);
axios
.post(
url
[,data[,config]]);
axios
.put(
url
[,data[,config]]);
axios
.patch(
url
[,data[,config]]);
注意:当我们在使用别名方法的时候,url,method,data这几个参数不需要在配置中声明。
axios请求的配置(很多)
请求返回的内容
{
data:{},
status:200,
//从服务器返回的http状态文本
statusText:'OK',
//响应头信息
headers: {},
//`config`是在请求的时候的一些配置信息
config: {}
}