1. Create a new folder Util, create a new mm.jsx file inside the util
2. Use the Ajax inside to send the request, call promise back, return a Promise Object
- Request (param) {
- return New Promise (Resolve, reject) = {
- $.ajax ({
- Type:param.type | | 'get',
- Url:param.url | | ‘‘,
- DataType:param.dataType | | ' JSON ',
- Data:param.data | | null,
- Success:res = {
- //Data request succeeded
- if (0 = = = res.status) {
- typeof Resolve = = = ' function ' && resolve (Res.data, res.msg);
- }
- //No sign-in status, Force login
- Else if (Ten = = Res.status) {
- this. Dologin ();
- }
- Else {
- typeof Reject = = = ' function ' && reject (res.msg | | res.data);
- }
- },
- Error:err = {
- typeof Reject = = = ' function ' && reject (Err.statustext);
- }
- });
- });
- }
3. Page Introduction
- const _MM = new mutil ();
4. Use, incoming parameters
- Home Data statistics
- Gethomecount () {
- return _mm.request ({
- URL: '/manage/statistic/base_count. do '
- });
- }
React backend Management system-ajax request encapsulation