0

In my controller if I return a JSON response like:

return new JsonResponse(array('numberOfRatings' => count($ratingCollection), 'oldRating' => $oldRating));

The returning object will have the following data:

protected 'data' => string '{"numberOfRatings":1,"oldRating":2}' (length=35)

But when I try to parse this with jQuery.parseJson(); it will return me an exception that jQuery is not able to parse it. But when I do:

return new JsonResponse(json_encode(array('numberOfRatings' => count($ratingCollection), 'oldRating' => $oldRating)));

What's equal to

return new Response(json_encode(array('numberOfRatings' => count($ratingCollection), 'oldRating' => $oldRating)));

The parseJson() method works great. But my mistake here cause it seems like JsonResponse is useless.

asked Jan 29, 2013 at 13:17

1 Answer 1

4

When you use JsonResponse, you don't need to use jQuery.parseJson(), the data you got is already a javascript object.

$.getJSON(your_url, function(data) {
 // the data is already an object, don't need to parse it.
 // var data = $.parseJSON(data); 
 // ...
});
answered Jan 29, 2013 at 13:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.