Mock.js: Generate random data, intercept Ajax requests, for these two purposes, the document address:https://github.com/nuysoft/Mock/wiki
Without saying anything, let's go to the html code first:
Mock.mock( rurl, template )
Record data templates. When an Ajax request that matches the rurl is intercepted, the simulation data is generated based on the data template and returned as response data.
'name|count': string
A string is generated by repeating string, and the number of repetitions is equal to the count.
'name|min-max': number
Generate an integer greater than or equal to min and less than or equal to max,The attribute value number is only used to determine the type, I struggled with the values behind it for a long time at first.
So, we Ajax requestshttp://www.bai.com, returned a random number between 20-60 with a name of lzlzlz and an age of 20-60.
Mock.mock( rurl, rtype, function( options ) )
Record the function used to generate response data. When an Ajax request that matches rurl and rtype is intercepted, the function function(options) will be executed and the execution result will be returned as response data.
options
pointing to the Ajax option set requested this time,Contains three attributes: url, type, and body, where the body is encoded and converted, and we need to decode it.
When using $.post request, we cannot get the returned json object normally, you can set the third parameter of the method to json, as follows:
where data.id is a randomly generated guid of the mock.
(End)
|