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

猜你喜欢

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