1. Install and use mock in node environment
# Install
npm Install MOCKJS
Using
a mock var mock = require (' mockjs ')
var data = Mock.mock (the
value of the {//property list is an array containing 1 to 10 elements
' list|1- [{
///property ID is an id|+1, the starting value is 1, each increment of 1
' ': 1
}]}]
//
output result
console.log (json.stringify ( Data, NULL, 4))
2, intercept the AJAX request call
methods are as follows
Mock.mock (Rurl, Rtype?, Template|function (Options))
Method Description:
(1) Rurl: Optional parameters.
Represents a URL that needs to be blocked, either a URL string or a URL regular. such as/\/domain\/list\.json/, '/domian/list.json '.
(2) Rtype: Optional parameters.
Represents the type of Ajax request that needs to be blocked. such as GET, POST, put, DELETE, and so on.
(3) Template|function: Required parameters, only one of the items.
(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, containing the URL, type, and body three properties, see the XMLHttpRequest specification.
Tips
Starting with 1.0, mock.js blocks AJAX requests by overwriting and simulating native XMLHttpRequest, and no longer relies on Third-party Ajax ToolPak (such as JQuery, Zepto, etc.).
3. Intercept Ajax Request Timeout
Configure the behavior when blocking an Ajax request. The supported configuration items are: timeout.
(1) Mock.setup (settings)
(2) settings
Must be selected.
A collection of configuration items.
(3) Timeout
Optional.
Specifies the response time, in milliseconds, of the blocked Ajax request. The value can be a positive integer, such as 400, which means that the response content is returned after 400 milliseconds, or it can be a cross-style string, such as ' 200-600 ', which indicates a response time between 200 and 600 milliseconds. The default value is ' 10-100 '.
4. I understand the interception
using the same method name, go to the column section to specify the method. Modify this point by call to reach intercept.
Implementation principle
//Definition parent class
var mock_ajax = function (str) {
this.showname=function () {
console.log (str);
}
return this;
};
Define Subclass
var jquery_ajax = function (str) {
this.showname = function () {
console.log (' Ajax ');
}
return this;
};
Jquery_ajax ("). ShowName ();//-> Ajax
//change this point
to Mock_ajax.call (Jquery_ajax, '");
Call
Jquery_ajax.showname ();