The mock intercept axios
1. This is also the final requirement function. We assume that the data requested by AXIos asynchronously is not online or incomplete;
2. Mock request intercepts and randomly generates filled data for front-end design;
// Ajax request axios.request({method: 'get', url: 'https://cdn.liyanhui.com/data.json' }).then(res => { console.log(res.data); }); / / the mock interception, and mock randomly generated fill data. The mock (' https://cdn.liyanhui.com/data.json '{' list | 5-10' : [{' id | + 1:1, 'username' : '@cname', 'email' : '@email', 'price' : '@integer', 'gender' : '@boolean' }] })Copy the code
Practice code:
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="http://mockjs.com/dist/mock.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script> //ajax axios.request({ method : 'get', url : 'https://cdn.liyanhui.com/data.json' }).then(res => { console.log(res.data); }); / / mock intercept mock. Mock (' https://cdn.liyanhui.com/data.json '{' list | 5-10' : [{' id | + 1:1, 'username' : '@cname', 'email' : '@email', 'gender' : '@boolean', 'price' : '@integer' } ] }); </script> </head> <body> </body> </html>Copy the code