Javascript 未使用ajax定义google.visualization

Javascript 未使用ajax定义google.visualization,javascript,php,google-visualization,Javascript,Php,Google Visualization,当我运行以下命令时,我得到TypeError:google.visualization未定义[了解更多信息] 数据是从我的.php文件返回的,我可以在执行console.log时看到它。 我可能会被电话回电弄得不知所措,或者可能有人会这样做 在我使用ajax之前,我已经尝试加载可视化,但同样的错误。我也看过其他可能的答案,但我仍然无法让它发挥作用 这是我的代码: $(document).ready(function(){ console.log("hello world") //alert("

当我运行以下命令时,我得到
TypeError:google.visualization未定义[了解更多信息]
数据是从我的.php文件返回的,我可以在执行console.log时看到它。 我可能会被电话回电弄得不知所措,或者可能有人会这样做

在我使用ajax之前,我已经尝试加载可视化,但同样的错误。我也看过其他可能的答案,但我仍然无法让它发挥作用

这是我的代码:

$(document).ready(function(){

console.log("hello world")
//alert("result")

$.ajax({
    url:"data.php",
    dataType : "JSON",
    success : function(result) {
        google.charts.load('current', { 'packages':['corechart'] });
        google.charts.setOnLoadCallback(function() {
            console.log(result);
            drawChart(result);                  
        });
    }
}); 

function drawChart(result) {

    var data = new google.visualisation.Datatable();
    data.addColumn('string','Name');
    data.addColumn('number','Quantity');
    var dataArray=[];
    $.each(result, function(i, obj) {
        dataArray.push([ obj.name, parseInt(obj.quantity) ]);
    });

    data.addRows(dataArray);

    var piechart_options = {
        title : 'Pie Chart: How Much Products Sold By Last Night',
        width : 400,
        height : 300
    }
    var piechart = new google.visualisation.PieChart(document
            .getElementById('piechart_div'));
    piechart.draw(data, piechart_options)

    var barchart_options = {
        title : 'Bar Chart: How Much Products Sold By Last Night',
        width : 400,
        height : 300,
        legend : 'none'
    }
    var barchart = new google.visualisation.BarChart(document
            .getElementById('barchart_div'));               
    barchart.draw(data, barchart_options)
}           

}); 
我从DB查询中得到一个对象,所以我认为这部分是正确的,它是一个包含6个对象的数组:

0: Object { id: "1", name: "Product1", quantity: "2" }
​1: Object { id: "2", name: "Product2", quantity: "3" }
​2: Object { id: "3", name: "Product3", quantity: "4" }
​3: Object { id: "4", name: "Product4", quantity: "2" }
​4: Object { id: "5", name: "Product5", quantity: "6" }
​5: Object { id: "6", name: "Product6", quantity: "11" }
值得一提的是,我的php代码如下:

data.php

<?php
require_once 'database.php';
$stmt = $conn->prepare('select * from product');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
echo json_encode($results); 
?>

database.php

<?php
$conn = new PDO('mysql:host=192.168.99.100;dbname=demo','root', 'root');
?>

注意:版本类似,但略有不同

您可以实际使用

google.charts.load

代替

$(文档).ready

它应该是一个z,而不是s

可视化

不是

可视化

像这样试试

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  $.ajax({
    url:"data.php",
    dataType : "JSON",
    success : function(result) {
      drawChart(result);
    }
  });
});

function drawChart(result) {
    var data = new google.visualization.Datatable();
    data.addColumn('string','Name');
    data.addColumn('number','Quantity');
    $.each(result, function(i, obj) {
        data.addRow([ obj.name, parseInt(obj.quantity) ]);
    });

    var piechart_options = {
        title : 'Pie Chart: How Much Products Sold By Last Night',
        width : 400,
        height : 300
    }
    var piechart = new google.visualization.PieChart(document
            .getElementById('piechart_div'));
    piechart.draw(data, piechart_options)

    var barchart_options = {
        title : 'Bar Chart: How Much Products Sold By Last Night',
        width : 400,
        height : 300,
        legend : 'none'
    }
    var barchart = new google.visualization.BarChart(document
            .getElementById('barchart_div'));
    barchart.draw(data, barchart_options)
}

非常感谢。我尝试用z替换s,但是现在我得到了这个
类型错误:google.visualization.Datatable不是构造函数。然后我尝试使用
google.charts.load
代替,但得到了相同的构造函数错误。有什么明显的想法吗?谢谢,我明白了,就是这一行
var data=new google.visualization.Datatable()
在Datatable中应该是大写的
t
,例如
var data=new google.visualization.Datatable()详细信息…想想我该睡觉了:)