React backend Management system-ajax request encapsulation

Source: Internet
Author: User

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

  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 sign-in status, Force login
  14. Else if (Ten = = 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 Data statistics
    2. Gethomecount () {
    3. return _mm.request ({
    4. URL: '/manage/statistic/base_count. do '
    5. });
    6. }

React backend Management system-ajax request encapsulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.