Monday, April 5, 2010

Re: echo JSON

Its because you are calling $this->render().

I am 99% sure that doesn't return the content, it just triggers cakes
view rendering process.

On Apr 5, 4:05 pm, "Dave" <make.cake.b...@gmail.com> wrote:
> I had same issue but solved it with adding exit(); where you have return
> null;.
>
> Dave
>
> -----Original Message-----
> From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf
>
> Of cricket
> Sent: April-05-10 8:02 PM
> To: CakePHP
> Subject: echo JSON
>
> I'm having some trouble sending JSON back to a JS AJAX function. What I need
> to do is save some data, then send it back for previewing. The AJAX function
> should display it in a modal window. However, I also need to send back the
> model ID in the case that the record has only just been created. That way,
> the JS function can add the hidden model ID input so subsequent previews
> won't save a new record.
>
> Anyway, alerting the response shows I'm getting the JSON object, plus a 2nd
> string containing the rendered view. I can't figure out why Cake is echoing
> this.
>
> Here's what I've got:
>
> public function admin_preview($id = null) {
>         $this->layout = null;
>         $this->autoLayout = $this->autoRender = false;
>         $this->viewPath = 'elements/email/text';
>         Configure::write('debug', 0);
>
>         if (!empty($this->data))
>         {
>                 $data = $this->Newsletter->save($this->data);
>         }
>         else if (isset($id))
>         {
>                 $data = $this->Newsletter->read(null, $id);
>         }
>         else
>         {
>                 return null;
>         }
>
>         $data['Newsletter']['nice_date'] = niceDate(date('Y-m-d'));
>         $this->set(compact('data'));
>
>         echo json_encode(
>                 array(
>                         'id' => $this->Newsletter->id,
>                         'content' => $this->render('newsletter')
>                 )
>         );
>         return null;
> }
>
> This results in a proper JSON object with id & content, plus the rendered
> content again.
>
> I tried return json_encode(...) but, strangely, that results in just the
> rendered view. What's going on?!
>
> Check out the new CakePHP Questions sitehttp://cakeqs.organd 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 cake-php@googlegroups.com To
> unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com For more options, visit this group
> cake-php+athttp://groups.google.com/group/cake-php?hl=en
>
> To unsubscribe, reply using "remove me" as the subject.

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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: