简体   繁体   English

来自php文件的Ajax调用

[英]Ajax call from php file

I am trying to retrieve data from a php page to use in a flot chart. 我正在尝试从php页面中检索数据以在浮动图表中使用。 I am having trouble with the ajax call and I cannot figure it out. 我在使用ajax调用时遇到问题,无法解决。

Here is some of my php: 这是我的一些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); 

Which outputs: 哪个输出:

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

Here is my ajax: 这是我的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);
    }
});

I am trying to see if the data is passed through so on success I just have an alert. 我正在尝试查看数据是否通过,因此成功后我会发出警报。

When this is run I get the output: 运行此命令后,我得到输出:

[object Object]

Any help would be appreciated! 任何帮助,将不胜感激!

Probably a header issue you need to specify it before your echo output. 可能是标题问题,您需要在回显输出之前指定它。

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

Otherwise use JSON.parse(response); 否则使用JSON.parse(response);

Data is an array, containing one object. 数据是一个包含一个对象的数组。

To access object use data[0] , for lable data[0].label 要访问对象,请使用data[0] ,用于able data[0].label

Wondering if the null at end is part of response....if so will cause invalid json parse error 想知道null处的null是否是响应的一部分....如果这样会导致无效的json解析错误

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

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