手写一个axios方法

如何实现一个axios函数呢,我们可以用原生 XMLHttpRequest 来实现一个简易版的 axios,如下:

javascript 复制代码
function axios(config) {
  return new Promise((resolve, reject) => {
    const { method = 'GET', url, data, headers = {}, timeout = 0 } = config;
    const xhr = new XMLHttpRequest();

    // 设置请求方法和URL
    xhr.open(method, url, true);

    // 设置请求头
    Object.keys(headers).forEach(key => {
      xhr.setRequestHeader(key, headers[key]);
    });

    // 设置超时
    xhr.timeout = timeout;

    // 处理响应
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4) {
        try {
          if (xhr.status >= 200 && xhr.status < 300) {
            resolve({
              data: JSON.parse(xhr.responseText),
              status: xhr.status,
              statusText: xhr.statusText,
              headers: parseHeaders(xhr.getAllResponseHeaders()),
              config: config,
              request: xhr
            });
          } else {
            reject({
              status: xhr.status,
              statusText: xhr.statusText,
              data: xhr.responseText,
              config: config,
              request: xhr
            });
          }
        } catch (error) {
          reject({
            status: xhr.status,
            statusText: xhr.statusText,
            data: xhr.responseText,
            config: config,
            request: xhr,
            error: error.message
          });
        }
      }
    };

    // 处理网络错误
    xhr.onerror = function() {
      reject(new Error('Network Error'));
    };

    // 处理超时
    xhr.ontimeout = function() {
      reject(new Error(`Request timed out after ${timeout}ms`));
    };

    // 发送请求
    xhr.send(data ? JSON.stringify(data) : null);
  });
}

// 辅助函数:解析响应头
function parseHeaders(headersString) {
  const headers = {};
  if (!headersString) return headers;

  headersString.split('\r\n').forEach(header => {
    const [key, value] = header.split(': ');
    if (key) {
      headers[key] = value;
    }
  });

  return headers;
}

// 使用示例
axios({
  method: 'GET',
  url: 'https://jsonplaceholder.typicode.com/posts',
  headers: {
    'Authorization': 'Bearer your-token'
  },
  timeout: 5000
})
  .then(response => console.log(response))
  .catch(error => console.error(error));

axios实现说明:

概述

axios 是一个封装了 XMLHttpRequest 的函数,用于发送 HTTP 请求。它返回一个 Promise,使得异步操作更加简洁和易于处理。

参数
  • config:一个对象,包含请求的各种配置项。
    • method:请求方法,默认为 'GET'
    • url:请求的 URL。
    • data:请求体数据,通常用于 POSTPUT 请求。
    • headers:请求头,默认为空对象 {}
    • timeout:请求超时时间,默认为 0(表示没有超时限制)。
1. 创建 XMLHttpRequest 对象
javascript 复制代码
const xhr = new XMLHttpRequest();
2. 设置请求方法和 URL
javascript 复制代码
xhr.open(method, url, true);
  • method:请求方法(如 'GET''POST' 等)。
  • url:请求的 URL。
  • true:表示请求是异步的。
3. 设置请求头
javascript 复制代码
Object.keys(headers).forEach(key => {
  xhr.setRequestHeader(key, headers[key]);
});

遍历 headers 对象的键,使用 setRequestHeader 方法设置每个请求头。

4. 设置超时
javascript 复制代码
xhr.timeout = timeout;

设置请求的超时时间(单位为毫秒)。

5. 处理响应
javascript 复制代码
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4) {
    try {
      if (xhr.status >= 200 && xhr.status < 300) {
        resolve({
          data: JSON.parse(xhr.responseText),
          status: xhr.status,
          statusText: xhr.statusText,
          headers: parseHeaders(xhr.getAllResponseHeaders()),
          config: config,
          request: xhr
        });
      } else {
        reject({
          status: xhr.status,
          statusText: xhr.statusText,
          data: xhr.responseText,
          config: config,
          request: xhr
        });
      }
    } catch (error) {
      reject({
        status: xhr.status,
        statusText: xhr.statusText,
        data: xhr.responseText,
        config: config,
        request: xhr,
        error: error.message
      });
    }
  }
};
  • onreadystatechange:当 xhrreadyState 属性改变时触发。
  • xhr.readyState === 4:表示请求已完成。
  • xhr.status >= 200 && xhr.status < 300:表示请求成功。
    • 解析响应体 xhr.responseText 并将其转换为 JSON 对象。
    • 调用 resolve,将成功响应的数据返回。
  • 其他状态码:表示请求失败,调用 reject,将错误信息返回。
  • try-catch:捕获解析 JSON 时可能发生的错误,并调用 reject
6. 处理网络错误
javascript 复制代码
xhr.onerror = function() {
  reject(new Error('Network Error'));
};

当发生网络错误时,调用 reject,返回一个包含错误信息的 Error 对象。

7. 处理超时
javascript 复制代码
xhr.ontimeout = function() {
  reject(new Error(`Request timed out after ${timeout}ms`));
};

当请求超时时,调用 reject,返回一个包含超时信息的 Error 对象。

8. 发送请求
javascript 复制代码
xhr.send(data ? JSON.stringify(data) : null);
  • 如果 data 存在,则将其转换为 JSON 字符串并发送。
  • 否则,发送 null
9. 辅助函数:解析响应头
javascript 复制代码
function parseHeaders(headersString) {
  const headers = {};
  if (!headersString) return headers;

  headersString.split('\r\n').forEach(header => {
    const [key, value] = header.split(': ');
    if (key) {
      headers[key] = value;
    }
  });

  return headers;
}
  • headersString:从 xhr.getAllResponseHeaders() 获取的响应头字符串。
  • 将响应头字符串按行分割,每行再按 : 分割,提取键值对并存入 headers 对象中。
10. 使用示例
javascript 复制代码
axios({
  method: 'GET',
  url: 'https://jsonplaceholder.typicode.com/posts',
  headers: {
    'Authorization': 'Bearer your-token'
  },
  timeout: 5000
})
  .then(response => console.log(response))
  .catch(error => console.error(error));
  • 发送一个 GET 请求到指定的 URL。
  • 设置请求头 Authorization
  • 设置请求超时时间为 5000 毫秒。
  • 使用 then 处理成功响应,使用 catch 处理错误。
相关推荐
知识分享小能手2 小时前
uni-app 入门学习教程,从入门到精通,uni-app基础扩展 —— 详细知识点与案例(3)
vue.js·学习·ui·微信小程序·小程序·uni-app·编程
MC丶科3 小时前
【SpringBoot 快速上手实战系列】5 分钟用 Spring Boot 搭建一个用户管理系统(含前后端分离)!新手也能一次跑通!
java·vue.js·spring boot·后端
lijun_xiao20095 小时前
前端最新Vue2+Vue3基础入门到实战项目全套教程
前端
90后的晨仔6 小时前
Pinia 状态管理原理与实战全解析
前端·vue.js
杰克尼6 小时前
JavaWeb_p165部门管理
java·开发语言·前端
90后的晨仔6 小时前
Vue3 状态管理完全指南:从响应式 API 到 Pinia
前端·vue.js
90后的晨仔6 小时前
Vue 内置组件全解析:提升开发效率的五大神器
前端·vue.js
我胡为喜呀6 小时前
Vue3 中的 watch 和 watchEffect:如何优雅地监听数据变化
前端·javascript·vue.js
我登哥MVP6 小时前
Ajax 详解
java·前端·ajax·javaweb
非凡ghost7 小时前
Typora(跨平台MarkDown编辑器) v1.12.2 中文绿色版
前端·windows·智能手机·编辑器·软件需求