2016-05-28 14 views
-1

私の問題は、このメソッドが真の結果を返さないということです。 入力の電子メールが自分のエンティティに存在するかどうかをテストします。ここでSymfony2でAjaxリクエストを作成

はコントローラです:

public function verificationAction(Request $request) 
{ 
    if ($this->container->get('request')->isXmlHttpRequest()) { 
     $email=$request->request->get('email'); 

     $em=$this->getDoctrine()->getEntityManager(); 
     $resp= $em->getRepository("CMSiteBundle:Prospect")->findBy(array('email'=>$email)); 
     $response =new Response(json_encode($resp)); 
     $response->headers->set('Content-Type', 'application/json'); 
     return $response; 
    } 
} 

答えて

0

あなたは古いトリックを試みることができます。 ;-)

...

<?php 

     class ABCController { 

      public function verificationAction(Request $request) { 
       if ($this->container->get('request')->isXmlHttpRequest()) { 
        $email = $request->request->get('email'); 

        $em  = $this->getDoctrine()->getEntityManager(); 
        $resp  = $em->getRepository("CMSiteBundle:Prospect") 
            ->findBy(array('email' => $email)); 
        //$response = new Response(json_encode($resp)); 
        //$response->headers->set('Content-Type', 'application/json'); 
        // THE TRICK IS THAT DIE RUNS FIRST 
        // THUS SENDS YOUR RESPONSE YOU THEREBY 
        // STOPPING THE RETURN FROM FIRING... ;-) 
        return die(json_encode($resp)); 
       } 
      } 
     } 

おそらく、この非常に古いトリックはまだあなたのために働く:Symfonyのコントローラのアクションであるので、あなたはそれほど好きではない偽DEAD応答なぜレスポンスを返す必要があります

関連する問題