如何通过ajax调用获取值回报?

时间:2017-08-30 02:41:05

标签: javascript php

我想从数据(ajax调用)中绘制谷歌图表

var jsonPieChartData = $.ajax({
                url: "ajax.php",
                data: {selValue1 : 1,selValue2 : 1} ,
                dataType: "json",
                async: false
                }).responseText;
        //create our data table out of json data loaded from server
        console.log(jsonPieChartData);

这是返回值

的ajax.php文件
function interncompany(){

global $DB;

//query by experience total post
$sql = 'SELECT lc.id, count(ljj.job_id) as count, lc.companyname FROM {local_jobs_job} ljj INNER JOIN {local_companydetail} lc ON ljj.job_company_userid = lc.userid  where ljj.job_type = 1 group by lc.companyname';


//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->companyname, 'f' => null],
    (object) ['v' => intval($item->count), 'f' => null]
]];
}, array_values($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);

}

使用选择框

动态选择数据
if ($select1 == '1') {

     if ($select2 == '1') {

               jobcompany();

                }

     if ($select2 == '2') {

                joblocation();
                }

      if ($select2 == '3') {
                jobcategory();
        }


      if ($select2 == '4') {
                jobsalary();
        }

      if ($select2 == '5') {
                jobexperience();
        }


        if ($select2 == '6') {
                joblevel();
        }

 }

 elseif ($select1 == '2') {

    if ($select2 == '1') {

                interncompany();

                }

     if ($select2 == '2') {
                internlocation();
                }

      if ($select2 == '3') {
                interncategory();
        }


      if ($select2 == '4') {
                internsalary();
        }

      if ($select2 == '5') {
                internexperience();
        }


        if ($select2 == '6') {
                internlevel();
        }
  }

我的问题:我想如何创建一个动态获取数据的函数并将结果插入数据:{},以便数据返回到php文件,以便可以通过ajax调用读取以绘制图表。

现在它什么也没有返回。我需要在ajax中硬编码内部数据:{}来绘制图表。

数据动态选择:

  // get the select value
            $(document).ready(function() {
            // for post-filter
                $('#post-filter').on('change',function(){
                var select1 = $(this).val();  // Post filter value
                var select2 = $("#field-filter").val(); // Field Filter value
                $.ajax({
                        type: 'POST',
                        url: 'ajax.php',
                        data: {selValue1 : select1,selValue2 :select2 },
                        success: function(result){
                           console.log(result); 
                        }
                    });
            });



             //  field filter value.
                 $('#field-filter').on('change',function(){

                    var select2 = $(this).val();  // Field filter value
                    var select1 = $("#post-filter").val(); // post Filter value
                    $.ajax({
                            type: 'POST',
                            url: 'ajax.php',
                            data: {selValue1 : select1,selValue2 :select2 },
                            success: function(result){
                               console.log(result);  
                            }
                        });
                     });        
            });

这是单击选择框时的结果...只有结果不会返回到ajax。填写内部数据的内容:{}将结果导入drawitem函数ajax调用?

enter image description here

插入标题时的错误

enter image description here

结果数据出现但出现错误,图表不存在。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法吗?我已经展示了字段过滤器的示例,如果它可以工作,您可以复制后过滤器

var dataVars = {};
dataVars['selValue1'] = $("#post-filter").val();  // Post filter value
dataVars['selValue2'] = $("#field-filter").val(); // Field Filter value


var jsonPieChartData = $.ajax({
                url: "ajax.php",
                data: JSON.stringify(dataVars) ,
                dataType: "json",
                async: false
                }).responseText;
}

还要确保在php中将内容类型标头设置为JSON

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