<!doctype html> <html lang="en"> <head> <Meta charset="UTF-8"> <title>两种不同写法的ajax请求数据</title> <script type="text/javascript" src="./js/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="./js/jquery1.42.min.js"></script> <script type="text/javascript" src="./js/jquery.min.js"></script> </head> <body> <input type="button" id="b1" value="ajax1"><br><br><br> <input type="button" id="b2" value="ajax2"><br><br><br> <span>ajax1异步获取的数据:<div id="ajax1"></div></span> <span>ajax2异步获取的数据:<div id="ajax2"></div></span> </body> </html> <script type="text/javascript"> $("#b1").click(function(){ htmlobj=$.ajax({url:"/ajax.PHP?type=1",async:false}); $("#ajax1").append(htmlobj.responseText); }); </script> <script type="text/javascript"> var url='ajax.PHP'; var getdata={ type:2 } $(document).ready(function(){ $("#b2").click(function(){ $.get(url,getdata,function(data){ $("#ajax2").append(data); }); }); }); </script>
2:ajax.PHP
<?PHP if ($_REQUEST['type'] == 1) { echo '我是ajax1异步获取的数据'; }elseif ($_REQUEST['type'] ==2) { echo '我是ajax2异步获取的数据'; }else{ echo '异常'; } ?>
3:如图
原文链接:https://www.f2er.com/ajax/164877.html