当前位置: 首页 > article >正文

Vue 中简易封装网络请求(Axios),包含请求拦截器和响应拦截器

Vue 中简易封装网络请求(Axios),包含请求拦截器和响应拦截器

axios简介

Axios 是一个基于 promise 的网络请求库,可以用于浏览器和 node.js

Axios官方中文文档

特性

  • 从浏览器创建 XMLHttpRequests
  • 从 node.js 创建 http 请求
  • 支持 Promise API
  • 拦截请求和响应
  • 转换请求和响应数据
  • 取消请求
  • 超时处理
  • 查询参数序列化支持嵌套项处理
  • 自动将请求体序列化为:
    JSON (application/json)
    Multipart / FormData (multipart/form-data)
    URL encoded form (application/x-www-form-urlencoded)
  • 将 HTML Form 转换成 JSON 进行请求
  • 自动转换JSON数据
  • 获取浏览器和 node.js 的请求进度,并提供额外的信息(速度、剩余时间)
  • 为 node.js 设置带宽限制
  • 兼容符合规范的 FormData 和 Blob(包括 node.js)
  • 客户端支持防御XSRF

安装

npm install axios;

示例代码

https.js

import axios from "axios";
// const token = localStorage.getItem("accessToken");

export const https = axios.create({
  baseURL: "http://localhost:3000",
  timeout: 15000,
  headers: {},
});

// 添加请求拦截器
https.interceptors.request.use(
  (config) => {
    // 在发送请求之前做些什么
    // if (token) {
    //   config.headers.accessToken = `Bearer ${token}`;
    // }
    return config;
  },
  (error) => {
    // 对请求错误做些什么
    // console.log(error);
    return Promise.reject(error);
  }
);

// 添加响应拦截器
https.interceptors.response.use(
  (response) => {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    // console.log(response);
    if (response.status === 200) {
      // console.log(Promise.resolve(response));
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
    // return response;
  },
  (error) => {
    // 超出 2xx 范围的状态码都会触发该函数。
    console.log(error);
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

在Vue中引入使用

import { https } from "@/api/http";
//GET请求
// 写过的一个分页查询为例
https
  .get("/display", {
    params: {
      pageSize: page.pageSize.value,
      currentPage: page.currentPage.value,
    },
  })
  .then((res) => {
    console.log(res);
  })
  .catch((error) => {
    console.log(error);
  });
  // 另一种写法
 https.get("/display?ID=12345")
  .then((res) => {
    console.log(res);
  })
  .catch((error) => {
    console.log(error);
  });
 
//POST请求
https
  .post("/display", {
  	id: id.value,
  })
  .then((res) => {
    console.log(res);
  })
  .catch((error) => {
    console.log(error);
  });

http://www.kler.cn/news/137222.html

相关文章:

  • 清朝笔记()
  • NVR小程序接入平台/设备EasyNVR多品牌NVR管理工具/设备的多维拓展与灵活应用
  • 电脑视频剪辑大比拼,谁更胜一筹?
  • RocketMQ快速开始
  • Elasticsearch 实战应用与优化策略研究
  • html 登入界面,用户注册界面相关的标签及案例
  • 如何优雅的避免空指针异常
  • SQL优化——如何写出高效率SQL
  • 如何在 ASP.NET Core 中使用 Quartz.NET
  • 哈希的应用——位图
  • 【shell脚本】全自动完成pxe无人值守批量装机脚本,匹配centos系列
  • Unity中Shader法线贴图(下)实现篇
  • 拉链表-spark版本
  • Python等于号标红怎么办,可能原因
  • React 自定义hook 之 防抖和节流
  • 很多人都在用的现货白银突破交易法 缺点需要注意
  • Qt Widget 自定义TitleBar带阴影窗口
  • 3PC(三阶段提交)
  • redis运维(七)基础通用命令
  • Flutter笔记:使用相机
  • 数字IC前端学习笔记:时钟切换电路
  • Idea2023 Springboot web项目正常启动,页面展示404解决办法
  • 论文《A recurrent latent variable model for sequential data》笔记:详解VRNN
  • 京东商品详情数据接口【京东API接口开发系列】,监控京东价格走势,接口代码示例,可高并发批量获取
  • 二百零四、Flume——登录监听窗口报错Ncat: bind to :::44444: Address already in use. QUITTING.
  • 005 OpenCV直方图