axios的使用

来源:互联网 发布:文本数据挖掘 编辑:程序博客网 时间:2024/05/19 23:24

二.axios的使用

1.安装

1>使用npm

npm install axios

2>使用bower

bower install axios

3>使用cdn

<script src="https://unpkg.com/axios/dist/axios.min.js"> </script>2.demo

2.1.get请求

axios.get('/user?ID=123')

.then(function(response){

console.log(response);

})

.catch(function(error){

console.log(error);

})

上面的也可以这样写:

axios.get('/user', {    params: {      ID: 123    }  })  .then(function (response) {    console.log(response);  })  .catch(function (error) {    console.log(error);  });

2.2.执行post请求

axios.post('/user',{

firstName:'Fred',

lastName:'Flintstone'

})

.then(function(response){

console.log(response);

})

.catch(function(error){

console.log(error);

})

2.3.执行多个并发请求

function getUserAccount(){

return axios.get('/user/123');

}

function getUserPermissions(){

return axios.get('/user/123/permissions');

}

axios.all([getUserAccount(),getUserPermissions()])

.then(axios.spread(function(acct,perms){

//两个请求现在都执行完成

}))

3.可以向axios传递相关配置来创建请求

axios(config)

//发送POST请求

axios({

method:'post',

url:'/user/123',

data:{

firstName:'Fred',

lastName:'Flintstone'

}

})

原创粉丝点击