Axios,基于Promise的HTTP客户端,可以工作于浏览器中,也可以在ode.js中使用。
功能:
从浏览器中创建XMLHttpRequest
从ode.js中创建http请求
支持PromiseAPI
拦截请求和响应
转换请求和响应数据
取消请求
自动转换JSON数据
客户端支持防止XSRF攻击
示例代码:
执行一个GET请求
// Make a request for a user with a give IDaxios.get('/user?ID=12345') .the(fuctio (respose) { cosole.log(respose); }) .catch(fuctio (error) { cosole.log(error); });// Optioally the request above could also be doe asaxios.get('/user', { params: { ID: 12345 } }) .the(fuctio (respose) { cosole.log(respose); }) .catch(fuctio (error) { cosole.log(error); });执行一个POST请求
axios.post('/user', { firstName: 'Fred', lastName: 'Flitstoe' }) .the(fuctio (respose) { cosole.log(respose); }) .catch(fuctio (error) { cosole.log(error); });执行多个并发请求
fuctio getUserAccout() { retur axios.get('/user/12345');}fuctio getUserPermissios() { retur axios.get('/user/12345/permissios');}axios.all([getUserAccout(), getUserPermissios()]) .the(axios.spread(fuctio (acct, perms) { // Both requests are ow complete }));
评论