0

我正在尝试让 javascript 加载和刷新实时数据。但我无法让它显示任何数据。

这是我的代码:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
 </head>

 <script type="text/javascript">

 function updateStats() {
    $.ajax({
        type: 'GET',
        url: "http://localhost/data.php", 
        dataType: 'json',
        success: function(data) {
            $('#sysstats').html(data.sysstats);
            $('#aststats').html(data.aststats);
            scheduleStatsUpdate();
        },
        error: updateFailed
    });
}
function scheduleStatsUpdate() {
    stats_timer = setTimeout('updateStats();',6000);
}
</script>

<body>


<div id="aststats" class="infobox ui-widget-content  ui-corner-all">

</div>
<div id="sysstats" class="infobox ui-widget-content  ui-corner-all">

</div>
</body>

Data.php 文件返回以下内容:

{"sysstats":"<h3 class=\"ui-widget-header  ui-state-default ui-corner-all\">System Statistics<\/h3><h4>Processor<\/h4><div class=\"databox\" style=\"width:200px;\">\n <div class=\"dataname\">Load Average<\/div>\n <div class=\"datavalue\"><a href=\"#\" title=\"Load Average: 0.01\">0.01<\/a><\/div>\n<\/div>\n<div class=\"databox graphbox\" style=\"width:200px;\" title=\"CPU: 0.00 \/ 100 (0%)\">\n <div class=\"bargraph graphok\" style=\"width:0px;\"><\/div>\n <div class=\"dataname\">CPU<\/div>\n <div class=\"datavalue\">0%<\/div>\n<\/div>\n<h4>Memory<\/h4><div class=\"databox graphbox\" style=\"width:200px;\" title=\"App Memory: 527.67MB \/ 2010.453125MB (26%)\">\n <div class=\"bargraph graphok\" style=\"width:52px;\"><\/div>\n <div class=\"dataname\">App Memory<\/div>\n <div class=\"datavalue\">26%<\/div>\n<\/div>\n<div class=\"databox graphbox\" style=\"width:200px;\" title=\"Swap: 0.00MB \/ 760.8828125MB (0%)\">\n <div class=\"bargraph graphok\" style=\"width:0px;\"><\/div>\n <div class=\"dataname\">Swap<\/div>\n <div class=\"datavalue\">0%<\/div>\n<\/div>\n<h4>Disks<\/h4><div class=\"databox graphbox\" style=\"width:200px;\" title=\"\/: 2.53GB \/ 30.18GB (8%)\">\n <div class=\"bargraph graphok\" style=\"width:16px;\"><\/div>\n <div class=\"dataname\">\/<\/div>\n <div class=\"datavalue\">8%<\/div>\n<\/div>\n<div class=\"databox graphbox\" style=\"width:200px;\" title=\"\/boot: 0.01GB \/ 0.10GB (10%)\">\n <div class=\"bargraph graphok\" style=\"width:20px;\"><\/div>\n <div class=\"dataname\">\/boot<\/div>\n <div class=\"datavalue\">10%<\/div>\n<\/div>\n<div class=\"databox graphbox\" style=\"width:200px;\" title=\"\/dev\/shm: 0.00GB \/ 0.98GB (0%)\">\n <div class=\"bargraph graphok\" style=\"width:0px;\"><\/div>\n <div class=\"dataname\">\/dev\/shm<\/div>\n <div class=\"datavalue\">0%<\/div>\n<\/div>\n<h4>Networks<\/h4><div class=\"databox\" style=\"width:200px;\">\n <div class=\"dataname\">eth0 receive<\/div>\n <div class=\"datavalue\"><a href=\"#\" title=\"eth0 receive: 0.00 KB\/s\">0.00 KB\/s<\/a><\/div>\n<\/div>\n<div class=\"databox\" style=\"width:200px;\">\n <div class=\"dataname\">eth0 transmit<\/div>\n <div class=\"datavalue\"><a href=\"#\" title=\"eth0 transmit: 0.00 KB\/s\">0.00 KB\/s<\/a><\/div>\n<\/div>\n","aststats":"<h3 class=\"ui-widget-header  ui-state-default ui-corner-all\">FreePBX Statistics<\/h3><div class=\"databox graphbox\" style=\"width:400px;\" title=\"Total active calls: 0 \/ 18 (0%)\">\n <div class=\"bargraph graphok\" style=\"width:0px;\"><\/div>\n <div class=\"dataname\">Total active calls<\/div>\n <div class=\"datavalue\">0<\/div>\n<\/div>\n<div class=\"databox graphbox\" style=\"width:400px;\" title=\"Internal calls: 0 \/ 18 (0%)\">\n <div class=\"bargraph graphok\" style=\"width:0px;\"><\/div>\n <div class=\"dataname\">Internal calls<\/div>\n <div class=\"datavalue\">0<\/div>\n<\/div>\n<div class=\"databox graphbox\" style=\"width:400px;\" title=\"External calls: 0 \/ 18 (0%)\">\n <div class=\"bargraph graphok\" style=\"width:0px;\"><\/div>\n <div class=\"dataname\">External calls<\/div>\n <div class=\"datavalue\">0<\/div>\n<\/div>\n<div class=\"databox graphbox\" style=\"width:400px;\" title=\"Total active channels: 0 \/ 36 (0%)\">\n <div class=\"bargraph graphok\" style=\"width:0px;\"><\/div>\n <div class=\"dataname\">Total active channels<\/div>\n <div class=\"datavalue\">0<\/div>\n<\/div>\n<h4>FreePBX Connections<\/h4>"}

我什至无法显示数据。我需要在我的服务器上安装什么特殊的东西来运行这个代码吗?

4

3 回答 3

0

您必须调用启动 ajax 调用或超时的函数,但我认为您想运行:

     setInterval(function() {
       updateStats();
     }, 6000);

反而。

于 2013-03-25T21:06:38.380 回答
0

为什么你打电话给 $('#sysstats').html(data.sysstats); ? ....我认为您没有任何具有“sysstats”ID 的元素...相反,您有“aststats”或“sysinfo”ID...。

于 2013-03-25T20:52:32.470 回答
0

改变:

function scheduleStatsUpdate() {
   stats_timer = setTimeout('updateStats();',6000);
}

经过

(function scheduleStatsUpdate() {
  stats_timer = setTimeout('updateStats();',6000);
})();
于 2013-03-25T21:41:44.017 回答