2014-01-21 3 views
0

자바 스크립트를 사용하여 색인 페이지에서 검색 기능을 구현하려고합니다. 나는 이름을 입력 할 때, 인덱스 페이지가 업데이트 얻을 것이다 serach을 적용하고 검색symfony의 js로보기로 데이터 전달

<div id="content"> 
    <form id="myForm" action="{{path('index_search')}}" method="POST" > 
    Write your name here: 
    <input type="text" name="name" id="name_id" value="" /><br /> 
    <input type="submit" value="Send" /> 
    </form> 
</div> 
<div id="output">#current index</div> 

여기 내 인덱스 페이지에서 양식을 여기

결과입니다 함께 새 인덱스 페이지를로드하는 양식을 가지고 근래 작업이 여기에

public function searchAction() 
     { 
     $request = $this->get('request'); 
     $name=$request->request->get('formName'); 

      $em = $this->getDoctrine()->getManager(); 
      $entities = $em->getRepository('SystemVmsBundle:VisitorsDetails')->findOneByfirstname($name); 

      $view = $this->render('SystemVmsBundle:VisitorsDetails:index.html.twig',array(
       'entities' => $entities, 
      )); 
      $return=array("responseCode"=>200, "view"=>$view); 

      $return=json_encode($return);//jscon encode the array 
     return new Response($return,200,array('Content-Type'=>'application/json')); 
} 

$(document).ready(function() { 

    //listen for the form beeing submitted 
    $("#myForm").submit(function(){ 
     //get the url for the form 
     var url=$("#myForm").attr("action"); 

     $.post(url,{ 
      formName:$("#name_id").val(), 
      other:"attributes" 
     },function(data){ 
      //the response is in the data variable 

      if(data.responseCode==200){ 
       $('#output').html(data.view); 

       $('#output').css("color","red"); 
      } 
      else{ 

       alert("An unexpeded error occured."); 


      } 
     }); 
     return false; 
    }); 


    }); 

그러나 내 JS 하시다 인 JS입니다 exexcuted입니다 rking하지만 js.js에보기로 데이터를 전달할 수 없습니다. js에 'index.html.twig'보기를 전달하는 방법은 무엇입니까? 불을 지르고와 함께 검사하면

, 내가

{"responseCode":200,"view":{"headers":{}}} 

어떤 아이디어처럼있어? 사전에 감사!

$.post(url, {formName: $("#name_id").val(), other: "attributes"}, 
    function(data) { 
    if(data.responseCode == 200){ 
     $('#output') 
     .html(data.view) 
     .css("color","red"); 
    } else { 
     alert("An unexpeded error occured."); 
    } 
    }, 'json'); 

을하지만 당신은 단지 JSON 내부의 HTML, 다른되지 않은 데이터가 필요한 경우, 당신은 정상 렌더링 논리를 변경해야합니다 :

답변

0

시도는 다음과 같이,을 $ .post 기능에 데이터 유형을 지정합니다 템플릿을 만들고 dataType 'html'을 사용하십시오. 이렇게 간다 :

// ...Controller.php 
$view = $this->render('SystemVmsBundle:VisitorsDetails:index.html.twig',array(
    'entities' => $entities, 
)); 
return new Response($view); 

// index.html.twig 
$.ajax({ 
    url: url, 
    dataType: 'html', 
    type: 'POST', 
    data: {formName: $("#name_id").val(), other:"attributes"}, 
    success: function (data) { 
    $('#output').html(data).css("color","red"); 
    } 
});