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.
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.
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.
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!