SUBSCRIBE VIA RSS


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.

Topics

Stack Overflow


The old fashioned way

RECENT TUNES

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:
sfContext::getInstance()->getResponse()->setContentType('text/javascript');
 
// from your action
$this->getResponse()->setContentType('text/javascript');
 
// as an action that returns some javascript directly
$this->getResponse()->setContentType('text/javascript');
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.