React后台管理系统-ajax请求封装

Posted giser123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React后台管理系统-ajax请求封装相关的知识,希望对你有一定的参考价值。

1.新建文件夹 util , 在util里边新建 mm.jsx文件

2.使用jquery里边的ajax发送请求,回调用promise,返回一个promise对象

  1. request(param){
  2.         return new Promise((resolve, reject) => {
  3.             $.ajax({
  4.                 type : param.type || ‘get‘,
  5.                 url : param.url || ‘‘,
  6.                 dataType : param.dataType || ‘json‘,
  7.                 data : param.data || null,
  8.                 success : res => {
  9.                     // 数据请求成功
  10.                     if(0 === res.status){
  11.                         typeof resolve === ‘function‘ && resolve(res.data, res.msg);
  12.                     }
  13.                     // 没有登录状态,强制登录
  14.                     else if(10 === res.status){
  15.                         this.doLogin();
  16.                     }
  17.                     else{
  18.                         typeof reject === ‘function‘ && reject(res.msg || res.data);
  19.                     }
  20.                 },
  21.                 error : err => {
  22.                     typeof reject === ‘function‘ && reject(err.statusText);
  23.                 }
  24.             });
  25.         });
  26.     }

3.页面引入

  1. const _mm = new MUtil();

4.使用,传入参数

  1. // 首页数据统计
  2.    getHomeCount(){
  3.        return _mm.request({
  4.            url: ‘/manage/statistic/base_count.do
  5.        });
  6.    }

以上是关于React后台管理系统-ajax请求封装的主要内容,如果未能解决你的问题,请参考以下文章

react_app 项目开发 _后台服务器端-node

后台管理系统相关

React ajax请求 及axiospubsubfetch使用

react封装基于axios的API请求

ajax请求后台交互json示例

react中向后台服务器发送一请求 后台接口返回的是byte[]类型的图片 我现在如何在前台界面中显示它?