symfony2 ajax简单请求

时间:2015-03-13 16:21:16

标签: javascript php ajax twig symfony-2.5

我正在使用Symfony2学习编程,我对AJAX请求有一些问题,但我不知道哪些是错误。

我尝试刷新2个组合框 Combobox1有一个课程名称列表 Combobox2必须根据ComboBox1 id选择进行刷新

的Javascript

function ListarProgramas(codfacu)
{
    $('#SelProgramas').html('cargando...');
    var datos = "IdFacultad="+codfacu;
    $.post(Routing.generate('bibliotecaportada_listar_programas'),
        {IdFacultad: codfacu},
        function(data)
        {
            if(data.responseCode == 200 )
            {
                $('#SelProgramas').html(data.ProgramasHtml);
            }
        }, "json");
}

 HTML 

<select name="SelEspecie" id="SelEspecie" onchange="ListarProgramas(this.value)" style="width: 350px;text-align: center">
  <option value="0">SELECCIONE FACULTAD</option>
  {% for facultad in facultades %}
       <option  value="{{ facultad.Id }}">{{ facultad.facultad }}</option>
  {% endfor %}
</select>

Controller

public function listar_programasAction()
{
  $request = $this->get('request');
  $IdFacultad = $request->request->get('IdFacultad');
  if($IdFacultad)
  {
    $repository = $this->getDoctrine()->getRepository("bibliotecaportadaBundle:Programas");
    $Programas = $repository->findBy(array('IdFacultad' => $IdFacultad));
    $ProgramasHtml = "";
    foreach ($Programas as $Programa)
    {
      $ProgramasHtml .= "<option>".$Programa->getProgProf()."</option>";
    }
    $return = array("responseCode"=>200, "ProgramasHtml" => $ProgramasHtml);
  }
  else
  {
    $return=array("responseCode"=>400, "info"=>"<option>SELECCIONE PROGRAMA</option>");
  }
  $return = json_encode($return);//jscon encode the array
  return new Response($return,200,array('Content-Type'=>'application/json'));//make sure it has the correct content type
}

0 个答案:

没有答案