Use mock.js to make front-end development independent of back-end
Nov 22, 2016 pm 12:35 PMFunction implemented by Mock.js.
Generate data based on data templates.
Generate data based on HTML template.
Intercept and simulate Ajax requests.
This article only demonstrates using mock.js to simulate and intercept Ajax requests.
First of all, quote in the page:
<script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script> <script src="http://mockjs.com/dist/mock.js"></script>
Define DIV:
<div> <h1 id="mockjs">mockjs</h1> </div>
The JS code is as follows:
<script type="text/javascript"> //調(diào)用mock方法模擬數(shù)據(jù) Mock.mock( 'http://mockjs', { "userName" : '@name', //模擬名稱 "age|1-100":100, //模擬年齡(1-100) "color" : "@color", //模擬色值 "date" : "@date('yyyy-MM-dd')", //模擬時間 "url" : "@url()", //模擬url "content" : "@cparagraph()" //模擬文本 } ); //ajax請求 $("#mockjs").click(function(){ $.ajax({ url : "http://mockjs", //請求的url地址 dataType : "json", //返回格式為json async : true, //請求是否異步,默認為異步,這也是ajax重要特性 data : {}, //參數(shù)值 type : "GET", //請求方式 beforeSend : function() { //請求前的處理 }, success: function(req) { //請求成功時處理 console.log(req); }, complete: function() { //請求完成的處理 }, error: function() { //請求出錯處理 } }); }); </script>
The running rendering is as follows:
You can see from the above rendering that the data is different every time.
If you want to know more Mock commands, you can check out the Mock.js official website: http://mockjs.com/
The above is just an introduction.
In order to make system management and use more convenient, everyone can learn about Ali RAP.
RAP is a visual interface management tool that analyzes the interface structure, dynamically generates simulation data, verifies the correctness of the real interface, and improves our collaboration efficiency through a series of automated tools around interface definition. Our slogan: Be more efficient and get home for dinner!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
