Where is the magic? 

What's so special?

 

Simply spitting out the results in JSON……… and?

 

 

Dave Maharaj

Freelance Designer | Developer
Description: header_logo
www.movepixels.com  |   <mailto:d...@movepixels.com> d...@movepixels.com  |  
709.800.0852

 

From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of 
Rubensvsx
Sent: Tuesday, January 21, 2014 12:29 PM
To: cake-php@googlegroups.com
Subject: Little trick to return json

 

Sorry for the poor english. 

I created a piece of code that solved my problem in returning a JSON object and 
helped me not to re-write all views. I await your feedback pros and cons.

 

 

class AppController extends Controller {
    public $components = array('RequestHandler');

 

    function beforeRender() {
        if ($this->RequestHandler->isAjax()) {
            $this->layout = 'ajax';
            $this->RequestHandler->setContent('json', 'application/json');
            $this->viewVars = json_encode($this->viewVars);
            return false;
        }
    }
}

 

 

If an Ajax request is performed automatically be returns a JSON object with the 
query result.

 

$.ajax({
url: "http://localhost/aplication/users";,
type: "POST",
dataType: "json"
});

-- 
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+unsubscr...@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/groups/opt_out.

-- 
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+unsubscr...@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/groups/opt_out.

<<image001.jpg>>

Reply via email to