2016-03-21 9 views
1

Symfony2のアクションによって返されるJSONデータオブジェクトのコンテンツをループしています。
AJAX下:「『636』を検索する演算子 『を』を使用することはできませんキャッチされない例外TypeError」:私は$アヤックスはまだ貴婦人エラーがスローされますです$ .getJSONを使用するかどうか
リクエストとデータが返さ: `
Symfony2 JSONオブジェクト - Error Uncaught TypeError: 'in'演算子を使用して '636'を検索できません

$(document).ready(function() { 
$('a.parents ').on('click', function (e) { 
e.preventDefault(); 
var newRow = '<table>\n\ 
<thead>\n\ 
<tr>\n\ 
<th>Nom</th>\n\ 
<th>Prenom(s)</th>\n\\n\ 
</tr>\n\ 
</thead>\n\ 
'; 
$.getJSON(Routing.generate($(this).attr('data-href'),{id: $(this).attr('data-id')}), 
function(data) { 
$.each(data.parents, function (i, parent) { 
    newRow += '<tr><td>' + parent[i].nom + '</td><td>' + parent[i].prenoms + '</td></tr>'; 
}); 
newRow += '</table>'; 
alert(newRow); 
}, 
'json'); 
}); 
}); 
` 

アクションによって返されたサンプルの結果:

01:
{"success":true,"parents":"{\u0022nom\u0022:\u0022Dafn\\u00e9e\u0022,\u0022prenoms\u0022:\u0022Graham\u0022},{\u0022nom\u0022:\u0022G\\u00e9raldine\u0022,\u0022prenoms\u0022:\u0022Phillips\u0022},{\u0022nom\u0022:\u0022H\\u00e9l\\u00e8na\u0022,\u0022prenoms\u0022:\u0022Hernandez\u0022},{\u0022nom\u0022:\u0022Ma\\u00eblla\u0022,\u0022prenoms\u0022:\u0022Perez\u0022},{\u0022nom\u0022:\u0022Illustr\\u00e9e\u0022,\u0022prenoms\u0022:\u0022Dixon\u0022},{\u0022nom\u0022:\u0022Ru\\u00ec\u0022,\u0022prenoms\u0022:\u0022Griffin\u0022},{\u0022nom\u0022:\u0022Rach\\u00e8le\u0022,\u0022prenoms\u0022:\u0022Kennedy\u0022},{\u0022nom\u0022:\u0022B\\u00e9n\\u00e9dicte\u0022,\u0022prenoms\u0022:\u0022Ferguson\u0022},{\u0022nom\u0022:\u0022Gis\\u00e8le\u0022,\u0022prenoms\u0022:\u0022Myers\u0022},{\u0022nom\u0022:\u0022Laur\\u00e9lie\u0022,\u0022prenoms\u0022:\u0022Mason\u0022},{\u0022nom\u0022:\u0022Y\\u00e9nora\u0022,\u0022prenoms\u0022:\u0022Olson\u0022},{\u0022nom\u0022:\u0022L\\u00e9onie\u0022,\u0022prenoms\u0022:\u0022Burns\u0022},{\u0022nom\u0022:\u0022Bj\\u00f6rn\u0022,\u0022prenoms\u0022:\u0022Vasquez\u0022},{\u0022nom\u0022:\u0022M\\u00e9lia\u0022,\u0022prenoms\u0022:\u0022Butler\u0022},{\u0022nom\u0022:\u0022R\\u00e1o\u0022,\u0022prenoms\u0022:\u0022Armstrong\u0022}]"}
を私は以下のように自分の行動でJMS Serializer Bundleを使用しています
public function trainingCandidatesAction(Request $request,$id) { 
     $request = $this->get('request'); 
    try{ 
    if ($request->isXmlHttpRequest() || $request->getMethod() == 'GET' || $request->getMethod() == 'POST') { 
      $em = $this->getDoctrine()->getManager(); 
      $array = $em->getRepository('ParentsBundle:Parents')->getParentsByFormation($id); 

      $serializer = $this->container->get('jms_serializer'); 
      $jsonContent = $serializer->serialize($array,'json'); 
      } 

      return new JsonResponse(array('success'=>true, 'parents'=>$jsonContent)); 
    }catch(\Execption $exception) 
    { 
     return new JsonResponse(array(
      'success' => false, 
      'code' => $exception->getCode(), 
      'message' => $exception->getMessage(), 
     )); 
    } 

     } 

私はstackoverflowで同様の質問から近づいてきましたが、それを正しく得ることができませんでした、私は何かが欠けている必要があります。

答えて

1

クリーンなJSONを取得するには、まず、あなたがJsonResponseをレンダリングする前に、あなたのデータをデコードする必要があります。

return new JsonResponse(array(
    'success'=>true, 
    'parents'=> json_decode($jsonContent), 
); 

あなたのデータは既にシリアル化され、そしてJsonResponseは再びそれらをコードするためです。

これでエラーが解決しない場合は、使用前にjsonを解析してみてください。 :

$.each(JSON.parse(data.parents), function(i, parent) { 
    // ... 
}); 

が、これはあなたのお役に立てば幸いです。

関連する問題