Tips_傳送請求時新增一個隨機數引數,讓瀏覽器每次都重新發請求到伺服器

LinSL16發表於2018-05-08
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>web_GET</title>
 6 </head>
 7 <body>
 8     <button class="btn">傳送GET請求</button>
 9     <script src="./jquery.min.js"></script>
10     <script>
11         var page = {
12             init : function(){
13                 this.bindEvent();
14             },
15             bindEvent : function(){
16                 var _this = this;
17                 $(document).on(`click`,`.btn`,function(){
18                     //產生一個5位的隨機數
19                     var rnd = _this.randomNum(5);
20                     console.log(rnd);
21                     $.ajax({
22                         type     : `get`,
23                         url      : `http://10.10.0.190:3000/WorkStat/workstat/get_user_latest_work_hist?rnd=`+rnd,
24                         dataType : `json`,
25                         data     : {
26                             user_id : 02519
27                         },
28                         success  : function(res){
29                             console.log(res);
30                         },
31                         error    : function(err){
32                             console.log(err);
33                         }
34 
35                     })
36                 })
37             },
38             //產生隨機數函式
39             randomNum : function(n){
40                 var randomNum = ``;
41                 for(var i=0; i<n; i++){
42                     randomNum += Math.floor(Math.random()*10);
43                 }
44                 return randomNum;
45             }
46         }
47         $(function(){
48             page.init();
49         })
50     </script>
51 </body>
52 </html>

 

相關文章