0. Install and use Mock in Node environment
# Install npm install mockjs
// Use Mockvar Mock = require('mockjs')var data = Mock.mock({ // The value of the attribute list is an array containing 1 to 10 elements 'list|1-10': [{ // The attribute id is an autoincrement number, the starting value is 1, each time increments 'id|+1': 1 }]})// The output result console.log(JSON.stringify(data, null, 4))1. Intercept ajax request call
The method is as follows
Mock.mock( rurl?, rtype?, template|function( options ) )
Method description:
(1) rurl: Optional parameters.
Indicates the URL to be intercepted, which can be a URL string or a URL regular. For example ///domain//list/.json/, '/domian/list.json'.
(2) rtype: optional parameters.
Indicates the type of Ajax request to be intercepted. For example, GET, POST, PUT, DELETE, etc.
(3) template|function: Required parameters, only one of them is taken.
(4) template represents a data template, which can be an object or a string. For example { 'data|1-10':[{}] }, '@EMAIL'.
(5) function points to the Ajax option set for this request, which contains three attributes: url, type and body. See the XMLHttpRequest specification.
hint
Starting in 1.0, Mock.js intercepts Ajax requests by overwriting and mocking native XMLHttpRequests, and no longer relies on third-party Ajax tool libraries (such as jQuery, Zepto, etc.).
2. Intercepting Ajax request timeout
Configure behavior when intercepting Ajax requests. Supported configuration items are: timeout.
(1) Mock.setup( settings )
(2) settings
Required.
Set of configuration items.
(3) timeout
Optional.
Specifies the response time of the intercepted Ajax request in milliseconds. The value can be a positive integer, such as 400, which means that the response content will not be returned after 400 milliseconds; or a string in the bar '-' style, such as '200-600', which means that the response time is between 200 and 600 milliseconds. The default value is '10-100'.
3. Interception I understand
Use the same method name and go to the column to specify the method. Modify this pointer through call to reach the interception.
// Implementation principle// Define the parent class var mock_ajax = function(str){ this.showName=function(){ console.log(str); } return this;};// Define the subclass var jquery_ajax = function(str){ this.showName = function(){ console.log('ajax'); } return this;};jquery_ajax('').showName();// -> ajax// Change this to point to mock_ajax.call(jquery_ajax,'111');// Call jquery_ajax.showName();