Ajax 请求

系统的ajax请求基于axios封装。

方法

基于restful规范,提供了5个方法:

  • get 获取服务端数据,参数拼接在url上,以 query string方式发送给后端
  • post 新增数据,参数以body形式发送给后端
  • put 修改数据,参数以body形式发送给后端
  • del 删除数据,参数拼接在url上,以params方式发送给后端
  • patch 修改部分数据,参数以body形式发送给后端

调用方式

可以通过三种方式,让React组件拿到ajax对象

  • config装饰器ajax属性
  1. import React, {Component} from 'react';
  2. import config from '@/commons/config-hoc';
  3. @config({
  4. ajax: true,
  5. ...
  6. })
  7. export default class SomePage extend Component {
  8. componentDidMount() {
  9. this.props.ajax
  10. .get(...)
  11. .then(...)
  12. }
  13. ...
  14. }
  • ajax装饰器
  1. import React, {Component} from 'react';
  2. import {ajaxHoc} from '@/commpons/ajax';
  3. @ajaxHoc()
  4. export default class SomePage extend Component {
  5. componentDidMount() {
  6. this.props.ajax
  7. .get(...)
  8. .then(...)
  9. }
  10. ...
  11. }
  • 直接引入ajax对象
  1. import React, {Component} from 'react';
  2. import {sxAjax} from '@/commpons/ajax';
  3. @ajaxHoc()
  4. export default class SomePage extend Component {
  5. componentDidMount() {
  6. sxAjax.post(...).then(...);
  7. // 组件卸载或者其他什么情况,需要打算ajax请求,可以用如下方式
  8. const ajax = sxAjax.get(...);
  9. ajax.then(...).finally(...);
  10. ajax.cancel();
  11. }
  12. ...
  13. }

注:config、ajaxHoc方式做了封装,页面被卸载之后会自动打断未完成的请求

参数

所有的ajax方法参数统一,都能够接受三个参数:

参数说明
url请求地址
params请求传递给后端的参数
options请求配置,即axios的配置,扩展了三个个:successTip errorTip,成功或失败提示;noEmpty过滤掉 ''、null、undefined的参数,不提交给后端

注:全局默认参数可以在src/commons/ajax.js中进行配置,默认baseURL='/api'、timeout=1000 * 60。

请求结果提示

系统对ajax失败做了自动提示,开发人员可通过src/commons/handle-error.js进行配置;

成功提示默认不显示,如果需要成功提示,可以配置successTip参数,或者.then()中自行处理;

成功提示在src/commons/handle-success.js中配置;

  1. this.props.ajax.del('/user/1', null, {successTip: '删除成功!', errorTip: '删除失败!', noEmpty: true});

loading处理

系统扩展了promise,提供了finally方法,用于无论成功还是失败,都要进行的处理。一般用于关闭loading

  1. this.setState({loading: true});
  2. this.props.ajax
  3. .get('/url')
  4. .then(...)
  5. .finally(() => this.setState({loading: false}));