create $output as a regular array and use the native json_encode function.

return $this->renderText(json_encode($output));

That way you can have something like this in your actions...

if ($request->isXmlHttpRequest())
{
  return $this->renderText(json_encode($output));
} else {
  return sfView::SUCCESS;
}

Which makes debugging a LOT easier ;)

Having said that, I've never been able to get JSON output to work using headers 
only - I've always just sent it as a regular response, without any layout or 
template.

----- Original Message -----
From: "SeeVik" <[email protected]>
To: "symfony users" <[email protected]>
Sent: Monday, 2 March, 2009 7:41:56 AM GMT +00:00 GMT Britain, Ireland, Portugal
Subject: [symfony-users] Problem in sending JSON as AJAX response...???


Hello all,

I am using Symfony 1.2.2 with doctrine plugin and jQuery. I am trying
to create an small application making use of AJAX helper
submit_to_remote(). My response contains JSON format string so I need
to create a callback.

http://www.symfony-project.org/book/1_0/11-Ajax-Integration#chapter_11_json

In this part of documentation I read that the JSON format was being
passed from action directly bypassing the view layer entirely using
the following code

$output = '[["title", "My basic letter"], ["name", "Mr Brown"]]';
$this->getResponse()->setHttpHeader("X-JSON", '('.$output.')');
return sfView::HEADER_ONLY;

I want to send the JSON data using $this->renderText() function...so
will changing the code like this work ???

$output = '[["title", "My basic letter"], ["name", "Mr Brown"]]';
$this->getResponse()->setContentType("text/x-json");   // I am
changing the response header type here
return $this->renderText( $output );   // I am sending the json data
here

This doesn't work for me if I have a callback like this...

function updateJSON(request, json)
{
   alert(json);     // this displays null
}

Am I setting contentType wrong? or is there something else that I have
goofed up?

thanks and regards
Vikram




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to