Axios 基于 Promise 的 HTTP 客户端开源项目

我要开发同款
匿名用户2016年11月06日
102阅读
开发技术JavaScript
所属分类服务器端JavaScript、Web应用开发、Node.js 扩展
授权协议MIT

作品详情

Axios,基于Promise的HTTP客户端,可以工作于浏览器中,也可以在node.js中使用。

功能:

从浏览器中创建XMLHttpRequest

从node.js中创建http请求

支持PromiseAPI

拦截请求和响应

转换请求和响应数据

取消请求

自动转换JSON数据

客户端支持防止XSRF攻击

示例代码:

执行一个GET请求

// Make a request for a user with a given IDaxios.get('/user?ID=12345')  .then(function (response) {    console.log(response);  })  .catch(function (error) {    console.log(error);  });// Optionally the request above could also be done asaxios.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');}function getUserPermissions() {  return axios.get('/user/12345/permissions');}axios.all([getUserAccount(), getUserPermissions()])  .then(axios.spread(function (acct, perms) {    // Both requests are now complete  }));
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论