Thursday, March 13, 2014

Re: How to return json message inside controller?

Thanks for this information

Wladimir Chópite
+584249700264
ve.linkedin.com/in/wchopite
"Mejor pirata de la armada". Hack the planet!

El 13/03/2014 07:08, "euromark" <dereuromark@gmail.com> escribió:
Not using json_encode() manually in the controller is the cleanest way.
So please see http://book.cakephp.org/2.0/en/views/json-and-xml-views.html on how to leverage JsonView instead
Also see http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/ for additional information.


Am Donnerstag, 13. März 2014 12:29:42 UTC+1 schrieb wchopite:

Use at the beginning of your controller function:

$this->autoRender=false;

Wladimir Chópite
+584249700264
ve.linkedin.com/in/wchopite
"Mejor pirata de la armada". Hack the planet!

El 13/03/2014 06:57, "Sam" <light...@gmail.com> escribió:


On Thursday, March 13, 2014 7:21:26 PM UTC+8, wchopite wrote:

Use the php function json_encode( ).

http://php.net/manual/en/function.json-encode.php

You can generate an associative array with the message you want, and use:

return json_encode($your_array);

Thanks. This worked. I can see the json message in the HTTP response. But I still have a problem. When I look at the HTTP response, it is full of other HTML code besides the json message. How can I remove the other HTML code and leave behind only a clean json message? 

Thank you.
 

Wladimir Chópite
+584249700264
ve.linkedin.com/in/wchopite
"Mejor pirata de la armada". Hack the planet!

El 13/03/2014 06:45, "Sam" <light...@gmail.com> escribió:

I have a controller which takes in an id and checks whether this id exists inside the Model. If it does not exist, I would like the controller to return a validation error message "id not found" in json when it returns the HTTP response. This controller takes in a normal HTTP POST which is not in json.

How can this be done in Cakephp 2.4.5?

My controller code looks something like this;

  public function controller_function($id=null)  {      if ($this->request->is('post'))       {         $field=$this->request->data['Model']['field'];         $Model_id = $this->Model->findFieldID($field);         if (empty($Model_id) ) //record not found. Return validation error         {             //Send validation error back in JSON. How??         }      }  }

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+u...@googlegroups.com.
To post to this group, send email to cake...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+u...@googlegroups.com.
To post to this group, send email to cake...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: