亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

無法生成靜態頁面

我將模板文件引入主文件中,同時模板文件的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";


正在回答

0 回答

舉報

0/150
提交
取消
PHP實現頁面靜態化
  • 參與學習       55615    人
  • 解答問題       118    個

網站開發必備技能—頁面靜態化,幫助大家掌握一項加薪利器

進入課程

無法生成靜態頁面

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號