無法生成靜態頁面
我將模板文件引入主文件中,同時模板文件的js地址以及AJAX獲取數據的php文件地址是根據主文件的地址來寫的相對地址,但是卻沒辦法將AJAX的內容寫入靜態的html文件中,不過我在主文件中使用ob_get_contents()是可以顯示AJAX數據的,這是為什么
以下三個文件都放在同一個目錄里
主文件代碼:
<?php echo?"設置時間觸發獲取靜態頁面"; if(file_exists("index.html")&&time()-filemtime("index.html")<30) { require("index.html"); }else{ $mysqli=new?mysqli("localhost","root",'',"test"); $sql="select?*?from?text"; $res=$mysqli->query($sql); $data=array(); while($row=$res->fetch_assoc()) { $data[]=$row; } ob_start(); require("index.php"); echo?file_put_contents("index.html",ob_get_contents()); ob_end_clean(); } 模板文件: <html?xmlns="http://www.w3.org/1999/xhtml"> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/> <script?src="jquery-1.11.2.min.js"></script> <title>無標題文檔</title> </head> <body> <table?style="border:solid?1px;"> <tr><th>id</th><th>name</th><th>age</th></tr> <?php?foreach($data?as?$v){?> <tr><td><?php?echo?$v['id']?></td><td><?php?echo?$v['name']?></td><td><?php?echo?$v['age']?></td></tr> <?php?}?> </table> <table?id="ajax"> </table> <script> $.ajax({ type?:"GET", url??:"ajax.php", datatype:?"json", success:function(msg) { msg=eval("("+msg+")"); if(msg.status=="success") { html=''; $.each(msg.data,function(key,val){ html+="<tr><td>"+val.id+"</td><td>"+val.name+"</td><td>"+val.num+"</td><td>"+val.location+"</td></tr>"; }); $("#ajax").html(html); } }, error:function() { alert("hehehhe"); } }); </script> </body> </html> ajax的數據來源:ajax.php <?php $mysqli=new?MySQLi("localhost","root",'',"test"); if($mysqli)?$connect=1; else?$connect=0; $sql="select?store.id,store.name,store.num,company.location?from?store?inner?join?company?on?store.id=company.id?order?by?store.num?desc?limit?6"; $res=$mysqli->query($sql); $data=array(); while($row=$res->fetch_assoc()) { $data[]=$row; } $message=array(); if(!empty($data)) { $message=array("mysqliConnect"=>$connect,"status"=>"success","data"=>$data); echo?json_encode($message); }else?echo?"fail";