ajax请求在服务器中不起作用后的JavaScript代码

时间:2016-02-03 12:45:20

标签: javascript php jquery ajax

我有一个将一些数据放入PHP文件的ajax请求,然后PHP文件生成带有一些JavaScript代码的HTML内容。 Javascript代码也包括Google的图表库。所以图表甚至不起作用。 (基本上ajax将一些数据发送到php文件,php文件生成图表)

ajax请求

$.ajax({ 
    type: 'GET', 
    url: 'report.php',
    data: { url: '<?php echo $_POST['url']; ?>' }, 
    success: function (data) { 
        var response = data;
        $('#to_change').html(data);
        clearInterval(interval);
    }
});
在report.php上

<div id="score">0</div>
<div id="chart1" style="width: 500px; height: 500px;"></div>
<script type="text/javascript">
        document.getElementById("score").innerHTML = "<?php echo $sum_score; ?>";
    </script>
    <script type="text/javascript">
          google.charts.load("current", {packages:["corechart","bar"]});
          google.charts.setOnLoadCallback(drawChart);
          function drawChart() {
            var data = google.visualization.arrayToDataTable([
              ['Pac Man', 'Percentage'],
              ['Analyzer Score', <?php echo $sum_score; ?>],
              ['', <?php echo 100-$sum_score; ?>],
              ['', 100]
            ]);

            var options = {
              pieHole: 0.5,
              tooltip: {text: 'value'},
              legend: 'none',
              width: 500,
              pieSliceText: 'none',
              pieStartAngle: 270,
              slices: {
                0: { color: '<?php echo $color; ?>' },
                1: { color: '#d8d8d8'},
                2: { color: 'yellow'}
              }
            };

            var chart = new google.visualization.PieChart(document.getElementById('chart1'));
            chart.draw(data, options);
          }
    </script>

我怀疑的一件事是它在localhost中完美运作!

4 个答案:

答案 0 :(得分:0)

数据资料:{ url: '<?php echo $_POST['url']; ?>' }使用其他变量,如

数据:{ data_url: '<?php echo $_POST['url']; ?>' }

答案 1 :(得分:0)

这给我看图有什么问题?

我没有在你的代码中看到

  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

图表工作完美!!

Parth Trivedi你不那么聪明,所以

Parth Trivedi let it as it is.as you can see how many bad answers are their... :D regarding url :D :D :D Parth Trivedi在聊天室写道

链接http://chat.stackoverflow.com/rooms/102477/discussion-between-parth-trivedi-and-gijo-varghese

   <? $color='black';
   $sum_score=5;
    echo 100-intval($sum_score); 

   ?>



   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <div id="score">0</div>
   <div id="chart1" style="width: 500px; height: 500px;"></div>
   <script type="text/javascript">
    document.getElementById("score").innerHTML = "<?php echo $sum_score; ?>";
</script>
<script type="text/javascript">
      google.charts.load("current", {packages:["corechart","bar"]});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Pac Man', 'Percentage'],
          ['Analyzer Score', <?php echo $sum_score; ?>],
          ['', <?php echo 100-$sum_score; ?>],
          ['', 100]
        ]);

        var options = {
          pieHole: 0.5,
          tooltip: {text: 'value'},
          legend: 'none',
          width: 500,
          pieSliceText: 'none',
          pieStartAngle: 270,
          slices: {
            0: { color: '<?php echo $color; ?>' },
            1: { color: '#d8d8d8'},
            2: { color: 'yellow'}
          }
        };

        var chart = new google.visualization.PieChart(document.getElementById('chart1'));
        chart.draw(data, options);
      }
 </script>

答案 2 :(得分:0)

我认为您必须首先确保所有DOM元素都已成功加载。无论您将<script>代码放在何处(开头,中间,末尾),浏览器始终会先加载它。

我建议您使用 jquery 。然后在<script>

中执行ff
<script type="text/javascript" src="jquery.js"></script>
<script>
   $(function(){
      // your js codes... just wrap them here
      // codes here are executed right after all html elements are rendered by browser
   });
</script>

答案 3 :(得分:0)

我解决了问题。代码中没有错误。我已经集成了CloudFlare CDN。他们为javascript文件实现了一个名为rocked loader的功能。这就是它无法正常工作的原因。我关掉了。一切都很好。