如何在PHP中的Json类型的ajax调用中获得响应?

时间:2017-10-31 08:44:39

标签: php jquery json ajax laravel

我无法获得ajax调用成功的数据。成功数据仍为空白。我怎样才能获得数据........................................... .................................................. .................................................. .................................................. ....

reports.blade.php

    $.ajax({
              type : "POST",
              url : '{{ url('filter_reports') }}',
              dataType: 'json',
              data : {
                'hours_from' : hours_from,
                'hours_to' : hours_to,
                'ddl_promotion' : ddl_promotion,
                'ddl_shops' : ddl_shops,
                'ddl_age_from' : ddl_age_from,
                'ddl_age_to' : ddl_age_to,
                'from_date' : from_date,
                'to_date' : to_date,
                'ddl_mall_center' : ddl_mall_center,
                'ddl_postal_code' : ddl_postal_code,
                'ddl_gender' : ddl_gender,
                'ddl_report_type' : ddl_report_type,
                'ddl_report_graph' : ddl_report_graph,
                '_token' : '{{ csrf_token() }}'
              },
              success : function(data)
              {
                if((ddl_report_type == 1 || ddl_report_type == 3 || ddl_report_type == 6 || ddl_report_type == 7) && ddl_report_graph == 1)
                {
                  $('#myChart').replaceWith('<canvas id="myChart" width="100" height="50"></canvas>');
                  var ctx = document.getElementById("myChart").getContext('2d');    
                  var myChart = new Chart(ctx, {
                    type: 'bar',
                    data: {
                      labels: **data.x,**
                      datasets: [{
                        label: 'Participants',
                        data: **data.y,**
                        backgroundColor: 'rgba(75,192,192,0.4)',
                        borderColor: 'rgba(75,192,192,1)',
                        borderWidth: 2
                      }]
                    },
                    options: {
                      title: {
                        display: true,
                        text: 'Participants'
                      },
                      scales: {
                        xAxes: [{
                          gridLines: {
                            display: false
                          }
                        }],
                        yAxes: [{ 
                          gridLines: {
                            display: false
                          },                    
                          ticks: {
                            beginAtZero:true,
                            min:**data.min,**
                            max:**data.max**
                          }                
                        }]
                      }
                    }
                  });
                }

ReportsController.php

if($report_graph == 1 || $report_graph == 2 || $report_graph == 3)
          {
            $get_data = DB::select("SELECT p.p_id,p.name,c.age FROM tbl_capture_img AS cap JOIN tbl_customers AS c ON c.c_id = cap.c_id JOIN tbl_promotions AS p ON p.p_id = cap.p_id WHERE cap.p_id = '{$promotion}'");
            $agerange = array('18-30','31-40','41-50','51-60','61-70','71-80','81-90','91-100');
            $final_age_range = '['.implode(',',$agerange).']';
            $agecount = array(0,0,0,0,0,0,0,0);
            foreach($get_data as $data)
            {
              if($data->age >=18 && $data->age <= 30)
              {
                $agecount[0] += 1;
              }
              else if($data->age >=31 && $data->age <= 40)
              {
                $agecount[1] += 1;
              }
              else if($data->age >=41 && $data->age <= 50)
              {
                $agecount[2] += 1;
              }
              else if($data->age >=51 && $data->age <= 60)
              {
                $agecount[3] += 1;
              }
              else if($data->age >=61 && $data->age <= 70)
              {
                $agecount[4] += 1;
              }
              else if($data->age >=71 && $data->age <= 80)
              {
                $agecount[5] += 1;
              }
              else if($data->age >=81 && $data->age <= 90)
              {
                $agecount[6] += 1;
              }
              else if($data->age >=91 && $data->age <= 100)
              {
                $agecount[7] += 1;
              }
            }
            $final_age_count = '['.implode(',',$agecount).']';
            $final['x'] = $final_age_range;
            $final['y'] = $final_age_count;

            $final['min'] = 0;
            $final['max'] = max($agecount) + 5;
            echo json_encode($final);
          }

输出

{"x":"[18-30,31-40,41-50,51-60,61-70,71-80,81-90,91-100]","y":"[4,0,0,0,0,0,0,0]","min":0,"max":9}

但我无法在 data.x,data.y,data.min,data.max 中获取数组,因此图形保持空白。我该怎么办?

2 个答案:

答案 0 :(得分:0)

在这种情况下,您需要对以下代码进行故障排除:

首先检查,使用console.log()的Ajax响应是正确的(甚至是json):

success : function(data)
{
    console.log(data);

如果它具有您期望的值,请确保阻止运行哪个和alert()或类似的console.log()。

if((ddl_report_type == 1 || ddl_report_type == 3 || ddl_report_type == 6 || ddl_report_type == 7) && ddl_report_graph == 1)
{
     alert("hi"); or console.log("hi");

如果一切正常,您无法访问标签中的数据: data.x,,您需要定义一个全局变量并在访问之后:

success : function(data)
{
    var gData = data;

并使用:

labels: **gData.x,**

如果每个步骤都被拒绝,则需要修复它,例如,如果数据没有任何值,则Laravel PHP控制器或通信结构中存在问题。

也可以将数据作为字符串获取,你需要在JavaScript中将其作为json(var parsedObjext = JSON.parse(data);)。

答案 1 :(得分:0)

在您的reports.blade.php文件中:在ajax成功中添加以下行,如下所示:

....
success : function(data)
          {
           data = jQuery.parseJSON(data);

 /*Add this above line and you can then access data.x and data.y*/


            if((ddl_report_type == 1 || ddl_report_type == 3 || ddl_report_type == 6 || ddl_report_type == 7) && ddl_report_graph == 1)
            {