简体   繁体   English

无法从AJAX请求中获取数字到javascript变量

[英]Having trouble getting a number from an AJAX request to a javascript variable

I'm attempting to make a chart that displays the CPU percentage on a page, and auto-updates along with the CPU percent as it changes. 我试图制作一个图表,在页面上显示CPU百分比,并随着CPU百分比的变化自动更新。 I'm having issues understanding how to get information back from the PHP script once the AJAX request has been sent. 我在理解如何在发送AJAX请求后如何从PHP脚本中获取信息时遇到问题。 Ideally I'd like to be able to use the variable '$cpuPercent' and have in the '.update();' 理想情况下,我希望能够使用变量“ $ cpuPercent”并包含在“ .update();”中。 part of my update function. 我的更新功能的一部分。

I've tried having the php code within the update function and echoing the $cpuPercent, but it does not update each time, as it'll only run once, and keep using the same number each time it updates. 我试过将PHP代码包含在update函数中并回显$ cpuPercent,但它不会每次都更新,因为它只会运行一次,并且每次更新时都使用相同的数字。

Here is the php from my system_actions.php script. 这是我的system_actions.php脚本中的php。

exec('wmic cpu get loadpercentage', $output2);
$cpuPercent = $output2[1];
echo json_encode($cpuPercent);

Here is my javascript 这是我的javascript

function getUsage(){
    $.ajax({
        url:'system_actions.php?action=cpu',
        async: true,
        dataType: 'json',
        type: 'post',
        success:function(output){
            var cpuPercent = parseInt(output);
            document.getElementById('cpu').value = cpuPercent;
            updateChart();
        }
    })
}
setInterval(getUsage, 2500);

function updateChart(){
    var link = document.getElementById('randombutton');
    link.click();
}

  $('.updatePieCharts').on('click', function(e) {
    e.preventDefault();
    charts.each(function() {
      var number = parseInt(document.getElementById('cpu').value);
      $(this).data('easyPieChart').update(number);
    });
  });

I definitely feel a disconnect in how it all works together, but I can't quite figure it out. 在所有功能如何协同工作方面,我绝对感到脱节,但我无法完全弄清楚。 Any help or a point in the right direction would be great. 任何帮助或朝着正确方向发展的观点都将非常有用。 Back to google I go. 回到谷歌我去。

Since your PHP script is returning JSON, the output variable in your success callback will be either a JavaScript object or array. 由于您的PHP脚本返回了JSON,因此success回调中的output变量将是JavaScript对象或数组。 Either way, you can't really use parseInt on it, so var cpuPercent is probably never getting set to anything useful. 无论哪种方式,您都不能真正在其上使用parseInt ,因此var cpuPercent可能永远不会设置为任何有用的东西。

If you post some sample output of your PHP script, we can tell you how to locate the number you're looking for. 如果您发布了PHP脚本的一些示例输出,我们可以告诉您如何找到所需的数字。

Try this. 尝试这个。

$.ajax({
    cache: false
})

And also you can try toggling your async option to false too. 另外,您也可以尝试将异步选项也切换为false。

I think this flot example may help you. 我认为这个例子可以为您提供帮助。 http://www.flotcharts.org/flot/examples/realtime/index.html http://www.flotcharts.org/flot/examples/realtime/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM