This article is a mirror article of machine translation, please click here to jump to the original article.

View: 10570|Reply: 0

[Other] mockjs simple tutorial

[Copy link]
Posted on 1/30/2019 10:35:36 AM | | | |
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)





Previous:Wednesday, January 30 Morning Post
Next:Introduction to the Darknet framework
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com