Home  >  Article  >  Web Front-end  >  Mock.JS intercepts HTTP request instance analysis

Mock.JS intercepts HTTP request instance analysis

小云云
小云云Original
2018-03-06 14:45:414168browse


MockJS is a relatively common front-end tool for simulating HTTP requests and responses. It can simulate various HTTP requests and return results. Realize the front-end simulation of the back-end interface without a back-end. The basic use of Mock is also relatively simple. In this article, we mainly share with you the analysis of Mock.JS interception of HTTP request examples, hoping to help everyone.

However, when MockJS is introduced,

import Mock from 'mockjs'

will intercept all HTTP requests sent by the front end. Regardless of whether Mock.mock is used to enable Mock simulation, HTTP requests will be intercepted.
This is why, even if Mock.mock is not used, the backend cannot obtain the front-end HTTP request.

Therefore, Once mockjs is referenced, HTTP requests cannot be made through the front end, but will be intercepted by mockjs.

Need to be in npm run build Before, remove the reference to mockjs.

Reference of Mock in 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);
    }
}

Only in Dev development environmentglobal.env === 'dev', introduce Mockjs to avoid release version, real HTTP requests are intercepted.

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

The problem of Mock.JS intercepting HTTP requests

MockJS is a relatively common front-end tool for simulating HTTP requests and responses. It can simulate various HTTP requests and return results. Realize the front-end simulation of the back-end interface without a back-end. The basic use of Mock is also relatively simple: Mock.JS official website
However, when MockJS is introduced,

import Mock from 'mockjs'

will intercept all HTTP requests sent by the front end, regardless of whether Mock.mock is used Enabling Mock simulation will intercept HTTP requests.
This is why, even if Mock.mock is not used, the backend cannot obtain the front-end HTTP request.

Therefore, Once mockjs is referenced, HTTP requests cannot be made through the front end, but will be intercepted by mockjs.

Need to be in npm run build Before, remove the reference to mockjs.

Reference of Mock in 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);
    }
}

Only in Dev development environmentglobal.env === 'dev', introduce Mockjs to avoid release version, real HTTP requests are intercepted.

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

Related recommendations:

Use mock.js to make front-end development and back-end independent

mock.js random data and use express output json interface example tutorial

Tutorial on using Mock.js to intercept AJAX requests in Node.js server environment

The above is the detailed content of Mock.JS intercepts HTTP request instance analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn