0

我需要显示一个包含来自服务器的一些数据的报告,这还包括一个图表。我试图在表单已经提交后使用 $.getJSON 发送 json 数据,但由于某种原因,我什至无法回显 $_GET [rep] 以确保我收到正确的数据.. 下面是 php 代码,这里是 js http://jsfiddle.net/f8rGb/2/

PHP代码

  if (isset($_POST["search"]) and $_POST["search"] == "go"){

     $name = $_POST['term'];

     $project = new Project();
     $pro = $project->get_project($name);  
     $serv = $project->get_project_service_by_id($pro);

     // DOING this to make sure i recieve the variable 
        // echo $_GET['rep'];

        if(isset($_GET['rep']))
        die(json_encode($serv)); 

     }

JS代码

   function searchProjectsRep() {

   // TRIGGERING THIS FUNCTION IN THE ONSUBMIT EVENT OF THE FORM

   var datos = $('#formProjectsRepSearch').serialize();
   var uri = $('#formProjectsRepSearch').attr('action');
    $.ajax({
        url: uri,
        data: datos,
        type: 'POST',
        success: function(resp) {
            $('#ajaxProjectsRep').html(resp);
            projectChart();
        }
 });
 }

 // CHART FUNCTION

 function projectChart(){

 $.getJSON('controller.php?rep=1', function(data) { 

    options = {
      chart: {
        renderTo: 'chart_div',
        defaultSeriesType: 'pie'
    },
    title: {
        text: 'Datos de proyecto'
    },

    series: []
    };
       var statusCount = {},
       serie1 = [];

       data.forEach(function(e) {
       statusCount[e.status] = 1 + (statusCount[e.status] || 0);
    });

    $.each(statusCount, function(status, count) {
    serie1.push( [status, count] );
    });
    options.series.push({data: serie1});

    new Highcharts.Chart(options);

    });
  }
4

1 回答 1

0

你只能做一个POST请求,或者一个GET请求,不能同时做两个,所以不要在GET检查POST请求是否完成的函数中返回变量的内容:

if ( isset($_POST["search"]) && trim( $_POST["search"] ) == "go" ){ 
    $name    = $_POST['term'];
    $project = new Project();
    $pro     = $project->get_project($name);  
    $serv    = $project->get_project_service_by_id($pro);
    exit;
}

if(isset($_GET['rep']))
    echo json_encode($serv); 
    exit;
}

另一方面,进行两次ajax调用有什么意义,只返回提交表单的第一个中的数据?

于 2013-06-17T19:54:46.623 回答