你能拥有多个ajax吗?

问题描述 投票:0回答:1

我正在使用ajax从慢速数据库中提取数据,这需要2.5秒。它成功更新了网页标签,但是当我尝试将其传递给Highcharts规格图时,毫不奇怪它失败了。我想如果我可以更新全局然后将它传递给它可以工作的图表,它没有。我试图延迟图表直到ajax完成,这失败了。我认为问题是我似乎无法更新全局。我想发布一些代码,但上次我试图将HTML放在这个网站上它出错了。

你的Simon M.

 // JavaScript Document
 // simple ajax to get data
    $(function () 
                {
                $.ajax({
                            url: 'extractLatest.php',   // call php file

                    success: function(datafromphp){ // when it is sucessful
        $( "#latest_mb" ).html(                 // replace html tag <div id="latest_mb"> contents 
                         datafromphp                                    // with whats returned from php
                            );


                                    var position    = "#gauge-chart-a"; // html container name
    // == start of chart == //
        $(function() {
          $(position).highcharts({
              chart: {
                type: 'gauge',
              },
              title: {
               text: 'Last Reading: '+ datafromphp +'mB at '+ msToTime(latest_UTC)
              },

                  pane: {
                startAngle: -150,
                endAngle: 150,
              },
              // the value axis
              yAxis: { // Month
                        // find scale max and min to nearest 10
                min: ((month_lo_mb/10)-0.5).toFixed(0)*10,
                max: ((month_hi_mb/10)+0.5).toFixed(0)*10,
                    title:{text:'mB'}, 

            plotBands: [

                    { // Month
        from: month_lo_mb,
          to: month_hi_mb,
        color: '#9DB6F9'}, // Blue

                    { // Week (Sun thro Sat)
        from: wk_lo_mb,
          to: wk_hi_mb,
        color: '#55BF3B'}, // green

                    { // Day
        from: day_lo_mb,
          to: day_hi_mb,
        color: '#FFFF63'}, // yellow

                    { // Day Avg
                // display day average line in red
        from: day_av_mb-0.125,
          to: day_av_mb+0.125,
        color: '#ff0000'} // red

                ]},
              series: [{ data: [datafromphp] }] // current
            }); // end of chart
        });
    }});
  });

这是我得到的最接近的东西

<?php
//------------------------------------------------------------------------- 
// Latest
 $latest_mb = mysqli_fetch_array(mysqli_query($dbconnect, "\n"
  ." SELECT UTC,mB \n"
  ." FROM thundersense  \n"
  ." ORDER BY utc DESC LIMIT 1"));

echo $latest_mb[1];
echo sprintf("<h1>Last reading: %1\$.3fmB<br></h1>",$latest_mb[1]);
echo "<h2><i> taken at " . date("H:i:s",$latest_mb[0]) . "</i></h2>";
?>

返回:

  < !--
  Start of data extraction
  -- >


982.976
javascript php html ajax highcharts
1个回答
0
投票

正如我所想,我缺乏经验,Ajax片段

     success: function(datafromphp){
     latest_array = JSON.parse(datafromphp);
     );

并添加到highcharts ...

    series: [{ data: [latest_array[1]] }]

PHP返回

echo json_encode($return,JSON_NUMERIC_CHECK);

现在它有效。只需要破解定期更新......你的Simon M.

© www.soinside.com 2019 - 2024. All rights reserved.