umi-request 一个新的请求工具

摘要:
只需封装npminstallumi请求--保存文档。有关具体封装,请参阅文档配置从“umi request”导入{extend};Constrequest=extend({prefix:“xxx”,//相当于baseurltimeout:10000,errorHandler,});functionerrorHandler(error){constcodeMap={500

简单封装

import { extend } from "umi-request";
const request = extend({
  prefix: "xxx",//相当于baseurl 
  timeout: 10000,
  errorHandler,
});

function errorHandler(error) {
  const codeMap = {
    500: "系统异常",
    404: "请求无效",
    403: "请求403",
  };
  if (error.response) {
    let status = codeMap[error.response.status];
    if (status) {
      alert(status);
    } else {
      throw error;
    }
  } else {
    throw error;
  }
  //
}

// request拦截器, 改变url 或 options.
request.interceptors.request.use((url, options) => {
  return {
    url,
    options: { ...options, interceptors: true },
  };
});

// 局部拦截器使用
request.interceptors.response.use((response, options) => {
  return response;
});

export default request;

// 使用
 import request from 'xxxx'
  request("/api/getMenu")
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    });


免责声明:文章转载自《umi-request 一个新的请求工具》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇js遍历json数据pyCharm中使用pytest结合allure运行报错module 'allure' has no attribute 'title'下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用http协议下载网络图片

     出于公司的一个需求,我要写一个下载网易微博的一个头像,但是网易微博的的图片是不允许外链的,所以我们只下载它,好了动手了,呵呵     一开始我用这样来读流   using (var stream = response.GetResponseStream())            {                //byte[] bytes...

在javaweb的项目当中实现随机数字的生成

首先,需要在javaweb的项目当中新建一个Servlet文件,然后再web.xml中配置一下: 这样运行的时候就可以通过“http://localhost:8080/Response/Response02”的地址访问到我们的Servlet。 开始代码的编写吧: 首先,我们需要写一个生成随即数字的方法,我们这次实现随即生成七位数的数字: priv...

Response.ContentType 详细列表

不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.代码如: <% response.ContentType ="text/html" %> <!--#i nclude virtual="/ContentType.html" --> 显示的为网页,而 <%...

Spring异常集中处理和日志集中打印

使用@ControllerAdvice和@ExceptionHandler处理Controller层的异常: @ControllerAdvice public class GlobalExceptionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(Gl...

vue springboot利用easypoi实现简单导出

vue springboot利用easypoi实现简单导出 前言 一、easypoi是什么? 二、使用步骤 1.传送门 2.前端vue 3.后端springboot 3.1编写实体类(我这里是dto,也一样) 3.2控制层 结尾 前言 今天玩了一下vue springboot利用easypoi实现excel的导出,以前没玩过导入导出,...

Django—中间件

CBV加装饰器(session) 要在CBV视图中使用我们上面的check_login装饰器,有以下三种方式: from django.utils.decorators import method_decorator from django.shortcuts import render,redirect,HttpResponse from django....