您的当前位置:首页axios专题(请求后台数据)

axios专题(请求后台数据)

来源:小侦探旅游网

Axios是一个基于promise的HTTP库,可以用在浏览器和nodejs中(参考axios中文网站)

安装

使用npm

npm install axios

执行GET请求

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);
  });

执行多个并发请求

function getUserAccount() {
	return axios.get('/user/12345');
}
fucntion getUserPermissions() {
	return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(),getUserPermissions()])
.then(axios.spread(function(acct,perms){
		//两个请求现在都完成
}))
axios API

可以通过向axios传递相关配置来创建请求
axios(config)

//发送post请求
axios({
	method: 'post',
	url: '/usr/12345',
	data: {
		firstName: 'Fred',
		lastName: 'Flintstone'
	}
});

因篇幅问题不能全部显示,请点此查看更多更全内容