1. 程式人生 > >jquery的get方式傳送AJAX請求

jquery的get方式傳送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 <script> 10 //* get方式向php傳遞引數name和age 2種方法 11 //* data為php返回值 可指定格式 12 /* 13 url:待載入頁面的URL地址 14 data:待發送 Key/value 引數。 15 callback:載入成功時回撥函式。 16 type:返回內容格式,xml, html, script, json, text, _default。 17 */ 18 $.get('./jquery_get.php?age=11
',{'name':'Harry'},function(data){ 19 $('#h1').append('<h2>'+ data.msg +'</h2>'); 20 },'json'); 21 </script> 22 <h1 id='h1'></h1> 23 </body> 24 </html>
jquery_get.html
 1 <?php
 2   //接受引數
 3   $name=$_GET['name'];
 4   $age
=$_GET['age']; 5 if ($name=='Harry' && $age==12) { 6 echo json_encode( 7 array( 8 'os'=>'windows', 9 'code'=>'php' 10 ) 11 ); 12 }else { 13 echo json_encode(array( 14 'code'=>404, 15 'msg'=>'not found' 16 ) 17 ); 18 } 19 ?>
jquery_get.php