jquery的ajax方式发送AJAX请求

 1 <!DOCTYPE html>
 2 <html>
 3   <head>
 4     <meta http-equiv="content-type" content="text/html;charset=utf-8" >
 5     <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js" ></script>
 6     <title></title>
 7   </head>
 8   <body>
 9     <button type="button" id="bid">test</button>
10     <script>
11       $('#bid').click(function(event) {
12         $.ajax({
13           url:'./jquery_ajax.php',
14           dataType:'json',
15           type:'GET',
16           timeout:2000,  //毫秒
17           success:function(data,status){
18             //* data 可能是 xmlDoc, jsonObj, html, text, 等等...
19           },
20           error:function(XMLHttpRequest, textStatus, errorThrown){
21             //* 通常 textStatus 和 errorThrown 之中,只有一个会包含信息
22             if (textStatus === 'timeout') {
23               alert('请求超时!');
24               setTimeout(function(){
25                 alert('准备重新请求!');
26               },2000);
27             }
28           }
29         });
30       });
31 
32     </script>
33   </body>
34 </html>
jquery_ajax.html
1 <?php
2   sleep(3);
3   echo json_encode(array('name'=>'Harry'));
4  ?>
jquery_ajax.php

猜你喜欢

转载自www.cnblogs.com/t-young1201/p/10231235.html