Thursday, March 13, 2014

Re: How to return json message inside controller?

You have an error in your array. The key 0 not exists. check the array

Consider also use what euromark says:

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.


2014-03-13 7:14 GMT-04:30 Sam <lightaiyee@gmail.com>:


On Thursday, March 13, 2014 7:29:42 PM UTC+8, wchopite wrote:

Use at the beginning of your controller function:

$this->autoRender=false;


Thanks. This worked. I still get some error message like this; 
Notice (8): Undefined offset: 0 [APP\\Model\\Model1.php, line 23] 
Are those cakephp debug messages? Can I remove them by setting the debug level down? 
 

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.



--
Atentamente:
Wladimir J. Chopite G.
+58 424-9700264 
Maturin, Venezuela

--
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: