2016-09-30 1 views
0

今日は荒い夜と午前のハハハです。私はdatasymfony2AJAX'POST'請願から得ることを試みています。[OLVED。 POST ajax請願書からのデータの取得中にエラーが発生するSymfony2

実際には配列を送信してコントローラで取得しますが、この変数のようなエラーが表示されますnullです。私は正しい方法でそれを得ることを試みていると思うが... 見よう!

これはJS AJAX申請です。私はul#sortable.children()命令をデータベースで更新するようになっているので、位置とIDの配列を送りました。

例:["1"、 "3"、 "2"]。したがって、db内のid = 1のオブジェクトは、1の位置に配置されます。id = 3の要素は2の位置にあり、id = 2の要素は3の位置に配置されます。

$('#ordenar').click(function(){ 
      var $children = $('#sortable').children(); 
      var numElementos = $children.length; 
      var socialSorted = []; 

      for(var i=0; i<numElementos; i++){ 
       socialSorted.push($children[i].id); 
      } 

      $.ajax({ 
       url: ' {{ path('admin_update_social_position') }} ', 
       datos: socialSorted, 
       method: 'POST' 
      }).done(function (data) { 
       if(data.type == 'OK'){ 
        window.location.reload(); 
       } 
       if(data.type == 'ERROR'){ 
        $('#error-message').slideDown(); 
       } 
      }); 
     }); 

そして、ここで私は多くの方法を試してみましコントローラ

/** 
    * @Route("/admin/update/order/social", name="admin_update_social_position") 
    */ 
    public function orderSocial(Request $request) 
    { 
     $em    = $this->getDoctrine()->getManager(); 

     $socialSorted = $request->request->get('socialSorted'); 
     $numElementos = count($socialSorted); 

     for($i=0; $i<$numElementos; $i++) 
     { 
      $social = $em->getRepository('AppBundle:Social')->findById($socialSorted[$i]); 
      $social->setPosition($i+1); 
      $em->persist($social); 
     } 

     if (empty($socialSorted)) { 
      $this->sendResponseStatus('ERROR'); 

      return new JSONResponse($this->getData()); 
     } 

     $em->flush(); 

     $this->sendResponseStatus('OK'); 

     // Generamos los datos para la respuesta ajax 
     return new JSONResponse($this->getData()); 
    } 

で...、配列を送信することで、配列でJSONを送信してからもデータを取得するためのさまざまな方法controller ...アドバイス/解決策?あなたのすべてに

おかげで、あなたは

EDIT:エラーがラインにあった

$social = $em->getRepository('AppBundle:Social')->findById($socialSorted[$i]); 
      $social->setPosition($i+1); 
      $em->persist($social); 

それはあなたとのArrayCollectionを返し、その後、私がすべきfindBy後Becausee:

$social = $em->getRepository('AppBundle:Social')->findById($socialSorted[$i]); 
       $social[0]->setPosition($i+1); 
       $em->persist($social[0]); 
+0

'datos:socialSorted、' ... datos? ...それは「データ:socialSorted」ではないでしょうか?あなたが 'null'を取ったという事実は、paramが正しく送られていないことを示します。 symfonysの 'request-> get()'は、paramが存在しなければ 'null'を返します。 –

+0

申し訳ありませんが間違っていれば私を修正してください。 ajaxでは、あなたは 'data'の代わりに' datos'を書いていますし、keynameでデータを送ることもできます。このように 'data:{'socialSorted':socialSorted}、 ' –

+0

同意してください!はい!どのような巨大な間違い.. haha​​hすべてにあなたに感謝!私は今、内部のエラーを受けていますが、私はコントローラを見てみましょう! @KinshukLahiri –

答えて

1

試用:

 $.ajax({ 
      url: ' {{ path('admin_update_social_position') }} ', 
      data: { 
       socialSorted: socialSorted, 
      }, 
      method: 'POST' 
     }).done(function (data) { 
      if(data.type == 'OK'){ 
       window.location.reload(); 
      } 
      if(data.type == 'ERROR'){ 
       $('#error-message').slideDown(); 
      } 
     }); 

データを配列として送信する必要があります。あなたは、要求でsocialSortedキーを頼むが、実際にあなたがそれを

+0

とにかく修正しましたが、私はまだコントローラにエラーがあります。この行:$ socialSorted = $ request-> request-> get( 'socialSorted');私はそれを印刷しているので、私にデータを与えていません:その理由は分かりますか? –

-1

を送信しないここでの問題は、あなたがAJAXからsocialSorted変数を期待しますが、それを得ることはありませんという事実にある:

コントローラで: $socialSorted = $request->get('socialSorted');

しかし、あなたのコントローラでAJAX呼び出し中... datos: socialSorted,

、ちょうどそれが読ませる... $socialSorted = $request->get('datos');またはにAJAX呼び出しを変更0

+0

なぜこの問題を解決するのか – renov8

関連する問題