我尝试使用FETCH从数据库中获取数据。我要做的是在页面上调用特定功能,从数据库中获取数据并在表中显示数据并传递给javascript。
下面是我的代码。
JavaScript代码
var url = "assets.php?fun=htmlcodefun";
fetch(url).then(function (response) {
return response.text();
}).then(function (html) {
console.log(html);
// Here I got page HTML
}).catch(function (err) {
console.log("Something went wrong!", err);
});
PHP代码
// Call the php function from the query string
if (isset($_REQUEST['fun'])) {
echo $_REQUEST['fun']();
}
// PHP function in which I'll get the data from DB and create an HTML table from that records.
function htmlcodefun(){
echo '<table>
<tr>
<td>1</td>
<td>Test</td>
</tr>
<tr>
<td>2</td>
<td>Demo</td>
</tr>
<tr>
<td>3</td>
<td>Test1</td>
</tr>
</table>';
}
我在javascript响应中得到了当前页面的HTML。
如果有人可以帮助我,那就太好了。
谢谢!