HTML頁面,ajax是基于id的,所有用id表示。
拿到的數(shù)據(jù)會(huì)顯示在這里
ajax源碼:
$(document).ready(function() {
$.ajax({
url : "admin/get_online_ganbu.php",//后臺(tái)請求的數(shù)據(jù),用的是PHP
dataType : "json",//數(shù)據(jù)格式
type : "post",//請求方式
async : false,//是否異步請求
success : function(data) { //如果請求成功,返回?cái)?shù)據(jù)。
var html = "";
for(var i=0;idata.length;i++){ //遍歷data數(shù)組
var ls = data[i];
html +="span>測試:"+ls.name+"/span>";
}
$("#test").html(html); //在html頁面id=test的標(biāo)簽里顯示html內(nèi)容
},
})
})
php源碼:
?php
include "conn.php";//這是鏈接數(shù)據(jù)的。
$result = mysql_query("SELECT * FROM online where class =1 ");
$dataarr = array();
while($row = mysql_fetch_array($result)){
array_push($dataarr, $row);
}
mysql_close($con);
echo json_encode($dataarr);
?>
以上這篇ajax實(shí)現(xiàn)從后臺(tái)拿數(shù)據(jù)顯示在HTML前端的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- JAVA后臺(tái)轉(zhuǎn)換成樹結(jié)構(gòu)數(shù)據(jù)返回給前端的實(shí)現(xiàn)方法
- java實(shí)現(xiàn)后臺(tái)數(shù)據(jù)顯示在前端
- SpringMVC實(shí)現(xiàn)前端后臺(tái)交互傳遞數(shù)據(jù)
- java后端把數(shù)據(jù)轉(zhuǎn)換為樹,map遞歸生成json樹,返回給前端(后臺(tái)轉(zhuǎn)換)
- javascript前端和后臺(tái)進(jìn)行數(shù)據(jù)交互方法示例