SUBSCRIBE VIA RSS


Subscribe to our feed

Topics

Stack Overflow


The old fashioned way

TWITTER

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.

Posted in  Web Development   |     |  delicious  Digg

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word