如何从ajax调用查询函数?

时间:2017-09-12 02:42:55

标签: jquery ajax

我想绘制一个谷歌图表,查询是用ajax.php文件中的函数编写的。该图表位于使用ajax调用json数据的不同php文件中。

我想在php文件中调用一个函数,joblocation()。

function joblocation(){

global $DB;

//query by location total post
$sql = 'SELECT ljl.location_id, count(ljj.job_id) as count, ljl.location_name FROM {local_jobs_job} ljj INNER JOIN {local_jobs_location} ljl ON ljj.job_location = ljl.location_id WHERE ljj.job_type = 0 GROUP BY ljl.location_name';


//get the query into record
$data = $DB->get_records_sql($sql);

//put the query into array
$rows = array();

$rows = array_map(function($item) {
return (object) ['c' => [
    (object) ['v' => $item->location_name, 'f' => null],
    (object) ['v' => intval($item->count), 'f' => null]
]];
}, array_values($data));


 // prepare return data
$cols = [
(object) ['id' => '', 'label' => 'LABEL', 'pattern' => '', 'type' => 'string'],
(object) ['id' => '', 'label' => 'TOTAL', 'pattern' => '', 'type' => 'number'],
];

$returndata = new stdClass;
$returndata->cols = $cols;
$returndata->rows = $rows;

echo json_encode($returndata);
}

这是调用该函数的ajax调用。

 var jsonColumnChartData = $.ajax({
                            url: "ajax.php",
                            contentType: "application/json",
                            data: {action: 'joblocation'},
                            dataType: "json",
                            async: false
                            }).responseText;

这样做是否正确?

我试过了:

var jsonPieChartData = $.ajax({
                            url: "ajax.php",
                            contentType: "application/json",
                            data: {},
                            dataType: "json",
                            async: false,
                            success: function(result){
                                joblocation(result);
                                },
                            error: function() {
                                alert('Error occured');
                                }
                            }).responseText;

当我跑步时,会弹出错误弹出窗口。它没有得到这个功能。

0 个答案:

没有答案