繁体   English   中英

来自php文件的Ajax调用

[英]Ajax call from php file

我正在尝试从php页面中检索数据以在浮动图表中使用。 我在使用ajax调用时遇到问题,无法解决。

这是我的一些php:

//Sort data into groups for chart
foreach($querieResult as $entry){
    if ($entry['GroupName'] == 'Blue'){
        $date = strtotime($entry['Date'] . ' UTC')*1000;
        $BlueData[] = array($date, $entry['OverallAverageHourlyEpisodes']);
    }    
}
//Put all data into single array to pass to JS file
$mergedData[]= array('label' => "Blue Team Data", 'data' => $BlueData);
//JSON encode data for JS file
echo json_encode($mergedData); 

哪个输出:

[{"label":"Blue Team Data","data":[[1373500800000,"1.57"],[1381276800000,"12.89"],[1377475200000,"28.04"]]}]null

这是我的ajax:

$.ajax({
    url:"getTeamPerformance.php",
    method: 'GET',
    cache: false,
    dataType: 'json',
    success: function(data){
        alert(data.label);
        alert(data.data);
    },
    error: function(errorGiven){
        document.write(errorGiven);
    }
});

我正在尝试查看数据是否通过,因此成功后我会发出警报。

运行此命令后,我得到输出:

[object Object]

任何帮助,将不胜感激!

可能是标题问题,您需要在回显输出之前指定它。

header('Content-type: application/json'); 

否则使用JSON.parse(response);

数据是一个包含一个对象的数组。

要访问对象,请使用data[0] ,用于able data[0].label

想知道null处的null是否是响应的一部分....如果这样会导致无效的json解析错误

暂无
暂无

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

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