Mock.js intercepting HTTP Request Instance parsing-JS notes

Source: Internet
Author: User

MOCKJS is a common tool for front-end analog HTTP request and reply, which can simulate various HTTP requests and return results. The front-end simulation of the backend interface is implemented in the case of no-end. Mock basic use is also relatively simple, this article we mainly share with you mock.js interception HTTP request instance resolution, hope to help everyone.

However, when the MOCKJS is introduced,
import Mock from ‘mockjs‘
Intercepts HTTP requests from all front ends, intercepting HTTP requests regardless of whether the mock emulation is turned on using Mock.mock.
This is why, even if not mock.mock, the backend cannot get the front-end HTTP request.

Therefore, once the MOCKJS is referenced, the HTTP request cannot be made through the front-end and will be intercepted by MOCKJS

You need to remove the reference to MOCKJS before the NPM run build.

References to a mock in the dev environment and release environment

import global from ‘../src/common/global‘;if (global.env === ‘dev‘){    var Mock = require(‘mockjs‘);}if (global.env === ‘dev‘){    //Run MOCK    for (let mockData of mockDatas){        //console.log(mockData);        Mock.mock(mockData.url, mockData.data);    }}

global.env = = = ' Dev ' in the dev development environment, introducing MOCKJS to avoid real HTTP requests being intercepted in release releases.

The global config variable is custom, not a global variable.

Mock.js Blocking HTTP request issues
MOCKJS is a common tool for front-end analog HTTP request and reply, which can simulate various HTTP requests and return results. The front-end simulation of the backend interface is implemented in the case of no-end. The basic use of Mock is also relatively simple: Mock.js official website
However, when the MOCKJS is introduced,
import Mock from ‘mockjs‘
Intercepts HTTP requests from all front ends, intercepting HTTP requests regardless of whether the mock emulation is turned on using Mock.mock.
This is why, even if not mock.mock, the backend cannot get the front-end HTTP request.

Therefore, once the MOCKJS is referenced, the HTTP request cannot be made through the front-end and will be intercepted by MOCKJS

You need to remove the reference to MOCKJS before the NPM run build.

References to a mock in the dev environment and release environment

  import global from ‘../src/common/global‘;if (global.env === ‘dev‘){    var Mock = require(‘mockjs‘);}if (global.env === ‘dev‘){    //Run MOCK    for (let mockData of mockDatas){        //console.log(mockData);        Mock.mock(mockData.url, mockData.data);    }}

global.env = = = ' Dev ' in the dev development environment, introducing MOCKJS to avoid real HTTP requests being intercepted in release releases.

The global config variable is custom, not a global variable.
In this share of the months I do the cylindrical wood template industry website: http://zhimo.yuanzhumuban.cc/

Mock.js intercepting HTTP Request Instance parsing-JS notes

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.