Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.


Stack Overflow

The old fashioned way


July 3, 2009 – 1:11pm Symfony: return javascript response

This is an old trick, but I still find it useful and will post it here in case it will help other symfony developers out there. If you are using the prototype.js library for your ajax requests, the Ajax.Request (and similar) utility methods will automatically evaluate the server’s response as javascript if the response has the ‘text/javascript’ content-type header. You can do this by setting your response headers like so:

// from within a template:
// from your action
// as an action that returns some javascript directly
return $this->renderText("alert('This is a javascript alert')");

This proves useful when you want to open up a modal window, show an alert, update multiple areas on a page, or just about anything else with javascript returned from your ajax request.

Comments are closed.