Skip to content
Advertisement

How can I send JSON response in symfony2 controller

I am using jQuery to edit my form which is built in Symfony.

I am showing the form in jQuery dialog and then submitting it.

Data is entering correctly in database.

But I don’t know whether I need to send some JSON back to jQuery. Actually I am bit confused with JSON thing.

Suppose I have added a row in my table with “jQuery and when I submit the form then after data is submitted I want to send back those row data so that I can dynamically add the table row to show the data added.

I am confused how can get that data back.

This is my current code:

JavaScript

This is just the template with success message.

Advertisement

Answer

Symfony 2.1

JavaScript

Symfony 2.2 and higher

You have special JsonResponse class, which serialises array to JSON:

JavaScript

But if your problem is How to serialize entity then you should have a look at JMSSerializerBundle

Assuming that you have it installed, you’ll have simply to do

JavaScript

You should also check for similar problems on StackOverflow:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement