创建实时Web应用程序

时间:2014-11-22 16:00:05

标签: javascript web highcharts xampp webserver

我必须创建一个使用Highcharts动态更新数据的应用程序。这不是一个大问题,因为他们有一个很好的教程。这个例子很好用。

当我想在多个设备上共享此应用程序时(使用xampp),我遇到了一些问题。当我打开指向我的webapp的链接时:

http://IP-adress/ENRGYMONITOR/index.html

图表显示,但无数据显示或更新。这是我写的javascript:

var chart; // global
/**
* Request data from the server, add it to the graph and set a timeout to request again
*/
function requestData() {
    $.ajax({
        url: 'http://localhost/live-server-data.php',
        success: function(point) {
            var series = chart.series[0],
            shift = series.data.length > 20; // shift if the series is longer than 20
            // add the point
            chart.series[0].addPoint(eval(point), true, shift);
            // call it again after one second
            setTimeout(requestData, 5000);
        },
        cache: false
    });
}

$(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            defaultSeriesType: 'spline',
            events: {
                load: requestData
            }
        },
        title: {
            text: 'Live random data'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150,
            maxZoom: 20 * 1000
        },
        yAxis: {
            minPadding: 0.2,
            maxPadding: 0.2,
            title: {
                text: 'Value',
                margin: 80
            }
        },
        series: [{
            name: 'Random data',
            data: []
        }]
    });
}); 

有谁能告诉我这是什么问题?

1 个答案:

答案 0 :(得分:0)

您的$.ajax来电失败了:

url: 'http://localhost/live-server-data.php'

"本地主机"这里是运行客户端的计算机(Web浏览器),而不是发送数据的服务器。

您应该尽量避免使用绝对网址。将其修改为:

 url: 'live-server-data.php'

假设(这很重要)live-server-data.phpindex.html位于同一目录中