React background management system -ajax request encapsulation React background management system -ajax request encapsulation

React background management system-ajax request encapsulation

1. Create a new folder util, create a new mm.jsx file inside util

2. Use ajax inside jquery to send the request, call the promise back, and return a promise object

  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.                     // Data request succeeded
  10.                     if(0 === res.status){
  11.                         typeof resolve === 'function' && resolve(res.data, res.msg);
  12.                     }
  13.                     // No login status, mandatory login
  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. Page introduction

  1. const _mm = new MUtil();

4. Use, incoming parameters

  1. // Home page statistics
  2.    getHomeCount(){
  3.        return _mm.request({
  4.            url: '/manage/statistic/base_count.do'
  5.        });
  6.    }

ON. Posted 2020-04-16 19:56   Six, hc   read ( ... ) Comments ( ...edit   collections

1. Create a new folder util, create a new mm.jsx file inside util

2. Use ajax inside jquery to send the request, call the promise back, and return a promise object

  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.                     // Data request succeeded
  10.                     if(0 === res.status){
  11.                         typeof resolve === 'function' && resolve(res.data, res.msg);
  12.                     }
  13.                     // No login status, mandatory login
  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. Page introduction

  1. const _mm = new MUtil();

4. Use, incoming parameters

  1. // Home page statistics
  2.    getHomeCount(){
  3.        return _mm.request({
  4.            url: '/manage/statistic/base_count.do'
  5.        });
  6.    }

Guess you like

Origin www.cnblogs.com/six-hc/p/12715258.html