我正在使用带有JavaScript的Google Visualization API来显示数据表。问题是:如果我创建一个包含所需内容的变量,我可以显示数据,但是当我从AJAX获取相同的数据时,我无法构建表。
以下是有效的代码:
function mostrarCuadro() {
var datos = [["Nombre","Altura","Apariencia","Blablabla","Cualidad","Presencia","Puntualidad","Talento"],["Leandro Leibovich",0,3,0,2,4,3,0],["Monica Bensusan",0,3,0,4,5,2,0],["Sergio Leibovich",2,2,3,3,3,4,2]];
datos = google.visualization.arrayToDataTable(datos);
// Create and draw the visualization.
visualization = new google.visualization.Table(document.getElementById('table_div'));
visualization.draw(datos, null);
}
在另一种情况下(不起作用),我使用AJAX jQuery从MySQL获取数据,使用PHP文件作为服务器端:
function obtenerData(){
document.getElementById("myDiv").innerHTML="Cargando...";
$(function ()
{
$.ajax({
url: 'obtieneCuadro.php', //the script to call to get data
data: "id=22755", //url argumnets
dataType: 'html', //data format
success: function(data) //on recieve of reply
{
var recibido = data;
datos = google.visualization.arrayToDataTable(recibido);
// Create and draw the visualization.
visualization = new google.visualization.Table(document.getElementById('table_div'));
visualization.draw(datos, null);
}
});
});
}
我的PHP文件返回(我在变量'recibido'中得到):
[["Nombre","Altura","Apariencia","Blablabla","Cualidad","Presencia","Puntualidad","Talento"],["Leandro Leibovich",0,3,0,2,4,3,0],["Monica Bensusan",0,3,0,4,5,2,0],["Sergio Leibovich",2,2,3,3,3,4,2]]
正如您所看到的,这两个变量具有相同的内容,但在第二种情况下它不起作用。任何人都可以告诉我这是什么问题,我怎样才能使它发挥作用?
谢谢。
编辑: 我已经设法解决了这个问题,现在它正常工作......我在这里展示了正确的代码:
<script type="text/javascript">
function obtenerData(){
document.getElementById("table_div").innerHTML="Cargando...";
$(function ()
{
$.ajax({
url: 'obtieneCuadro.php', //the script to call to get data
data: "id=22755", //url argumnets
dataType: 'html', //data format
success: function mostrarCuadro(data) //on recieve of reply
{
datosTabla = google.visualization.arrayToDataTable(JSON.parse(data));
// Create and draw the visualization.
visualization = new google.visualization.Table(document.getElementById('table_div'));
visualization.draw(datosTabla, null);
}
});
});
}
</script>
答案 0 :(得分:0)
我已设法解决问题,现在它正常工作......我留下了正确的代码:
<script type="text/javascript">
function obtenerData(){
document.getElementById("table_div").innerHTML="Cargando...";
$(function ()
{
$.ajax({
url: 'obtieneCuadro.php', //the script to call to get data
data: "id=22755", //url argumnets
dataType: 'html', //data format
success: function mostrarCuadro(data) //on recieve of reply
{
datosTabla = google.visualization.arrayToDataTable(JSON.parse(data));
// Create and draw the visualization.
visualization = new google.visualization.Table(document.getElementById('table_div'));
visualization.draw(datosTabla, null);
}
});
});
}
</script>
我必须使用&#34; JSON.parse(data)&#34;来解析数据。