Discussion:
Twig, JSON and AJAX
ApoY2k
2011-07-31 12:59:26 UTC
Permalink
I want to create an XHR-Controller that responds with JSON-Objects.

So I create an array in that controller an pass it to the twig-template.

$response = array('message' => 'hello');
return $this->render('MyBundle:Xhr:message.json.twig');

In the template, I encode the array using json_encode

{{ message|json_encode }}

But the returned template isn't recognized as a JSON-Object, but as a
string. It seems that the string is escaped and jQuery therefore doesn't
recognize it as an object, but as a normal string.

How can I get the JSON-Object in JavaScript to be parsed correctly?
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
Klaas Naaijkens
2011-07-31 15:06:43 UTC
Permalink
You can just as well return a response without a template.

$response = new Response();
$response->setContent(json_encode(...));
And set your content type with
$response->setHeader(...);

Sent from my iPad
Post by ApoY2k
I want to create an XHR-Controller that responds with JSON-Objects.
So I create an array in that controller an pass it to the twig-template.
$response = array('message' => 'hello');
return $this->render('MyBundle:Xhr:message.json.twig');
In the template, I encode the array using json_encode
{{ message|json_encode }}
But the returned template isn't recognized as a JSON-Object, but as a string. It seems that the string is escaped and jQuery therefore doesn't recognize it as an object, but as a normal string.
How can I get the JSON-Object in JavaScript to be parsed correctly?
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
Artem Shpakov
2011-08-01 05:20:29 UTC
Permalink
You can also specify an output format using routing configuration:

_myroute:
pattern: /my/route
defaults: { _controller: MyBundle:MyController:myaction, _format:
json }
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
Bart van den Burg
2011-08-01 08:06:59 UTC
Permalink
JQuery won't recognize your JSON as JSON if you don't sent the
appropriate content-type header: Content-type: text/json. Verify that
this header is correctly set.
Post by ApoY2k
I want to create an XHR-Controller that responds with JSON-Objects.
So I create an array in that controller an pass it to the twig-template.
$response = array('message' => 'hello');
return $this->render('MyBundle:Xhr:message.json.twig');
In the template, I encode the array using json_encode
{{ message|json_encode }}
But the returned template isn't recognized as a JSON-Object, but as a
string. It seems that the string is escaped and jQuery therefore doesn't
recognize it as an object, but as a normal string.
How can I get the JSON-Object in JavaScript to be parsed correctly?
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
ApoY2k
2011-08-01 12:22:44 UTC
Permalink
It was set correctly, the problem was, that I had to use the "raw" filter in
twig:

{{ response|json|raw }}

did the trick.
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
Deni V
2011-07-31 13:34:10 UTC
Permalink
Should be sent the valid content type in the response - application/json

You can create a new instance of Symfony\Component\HttpFoundation\Response
and pass it to the render() method as third argument:

$response = new Response();
$response->headers->set('Content-type', 'application/json; charset=utf-8');

return $this->render('MyBundle:Xhr:message.json.twig', array('message' =>
'hello'), $response);
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
John Bohn
2011-07-31 13:35:18 UTC
Permalink
Sorry, autocorrect. I meant, application/json.
Try setting the Content-type header to application/Jain
Post by ApoY2k
I want to create an XHR-Controller that responds with JSON-Objects.
So I create an array in that controller an pass it to the twig-template.
$response = array('message' => 'hello');
return $this->render('MyBundle:Xhr:message.json.twig');
In the template, I encode the array using json_encode
{{ message|json_encode }}
But the returned template isn't recognized as a JSON-Object, but as a string. It seems that the string is escaped and jQuery therefore doesn't recognize it as an object, but as a normal string.
How can I get the JSON-Object in JavaScript to be parsed correctly?
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
John Bohn
2011-07-31 13:33:21 UTC
Permalink
Try setting the Content-type header to application/Jain
Post by ApoY2k
I want to create an XHR-Controller that responds with JSON-Objects.
So I create an array in that controller an pass it to the twig-template.
$response = array('message' => 'hello');
return $this->render('MyBundle:Xhr:message.json.twig');
In the template, I encode the array using json_encode
{{ message|json_encode }}
But the returned template isn't recognized as a JSON-Object, but as a string. It seems that the string is escaped and jQuery therefore doesn't recognize it as an object, but as a normal string.
How can I get the JSON-Object in JavaScript to be parsed correctly?
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
MridulFlexi Magnetics
2012-11-15 05:01:19 UTC
Permalink
Hi!

I was trying to make a variable 'Amount in Words' for invoice Printing
Template...

i want to add {{invoice.grossinwords}}

Can you tell me where did you add these $response lines... to pass the
string!
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "Symfony2" group.
To post to this group, send email to symfony2-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony2+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony2?hl=en
Loading...