Thanks for your reply. I ended up using something similar to your
suggestion/
$response['html'] = $this->render('my_view_needed');
Then echo back my json_encode($response) and use response.html in the js to
render the view / messages / errors.
Not sure if this is the best way but it works.
Dave
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf
Of Chad
Sent: January-20-10 7:08 PM
To: CakePHP
Subject: Re: JSON View
Might have figured something out by now but here's what I did, no idea if
it's the right way or not but it works for me :)
I created a new layout, call it jsonp.ctp for example. Set the appropriate
controllers or methods to use this layout.
The layout is this:
<?php
$content = '<div id="widget-container">';
if ($session->check('Message.flash')) {
$content .= $session->read('Message.flash'); }
$content .= $content_for_layout;
$content .= '</div>';
echo $_GET['callback'] . '(' . json_encode($content) . ')'; ?>
Done. I wrap the output in a div common to all mock pages. Then as far as
the controller and views are concerned, it's business as usual and
everything just works. You could also opt to return an array with the flash
message as one param and an html param as another, depending on how the js
side of the app works. Your methods could also pass a status variable into
the view and the layout can pick that up and return it.
If you're not using cross domain ajax your layout could be a bit easier
then.
<?php echo json_encode( array( 'status' => $status, 'html' =>
$content_for_layout ) ); ?>
On Jan 11, 7:50 am, "Dave" <[email protected]> wrote:
> Does anyone know how to return aJSONobject from an ajax request to
> return the view and a success status? I see all these tutorials to
> retrn data but nohtml. Everything is layout = false, autoRender =
> false. I can return the view in normalhtmlbut I need to get a status =
> true or false to the js to tell it what to do next depending on the
> status. I need to update 1 of 2 possible divs with the response and
> that's why I need the success to tell the js if success update this div if
no success then do something different.
>
> I need to return a view along with my success or fail status. Not just
> an error message as I have found in myjson/ cakephp searching.
>
> so in the controller
> $this->layout = 'ajax';
> if(this->Model->save())
> {
> //return success = true and success view $respounse = array('status'
> => true, 'html' => ???
>
> } else {
>
> //return false
> $respounse = array('status' => false);}
>
> $this->header('Content-Type: application/json');
> echo json_encode($response);
>
> Ideas?
>
> Thanks
>
> Dave
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.730 / Virus Database: 271.1.1/2635 - Release Date: 01/20/10
15:48:00
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" 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/cake-php?hl=en