SUBSCRIBE VIA RSS


Subscribe to our feed

Topics

Stack Overflow


The old fashioned way

TWITTER

  • enjoying some diet coke and good tunes. 7 hrs ago
  • are we still supposed to cut those plastic rings that come with six packs before throwing them away? 1 week ago
  • does anyone know how the ipad might handle multiple users? like, say u want to have one at home with different email accounts for each user? 1 week ago
  • More updates...

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