1
votes

I'm trying to make a cakephp app where I'm trying to receive an array through JSON POST request, save it in a local array in controller and pass that array to json view where it can just give a json response with the same array. I've added mapresources and parseextensions in routers.php. It's giving me a 200ok response on hitting it with a POST Json request but it's not saving the data in local array, and also it's passing a whole html page appended with the json response. This is my controller:

class UsersController extends AppController{

    public $components = array('RequestHandler');    
    public function index(){
     }

    public function add(){    
        if($this->request->is('post')){
       $data = $this->User->save($this->request->data);
                $this->set(array(
                'data' => $data,
                '_serialize' => array('data')
            ));
    }}}

I named json view file add.ctp. This is JSON view in /app/view/Users/JSON/add.ctp :

<?php
echo json_encode($data);
?>

My table Users originally saves userdata in the form of name, password email etc. What's happening is when I'm hitting url /localhost/FC/app/users.json with following data {{"firstname":"abcrehd","lastname":"sdfsdf"} It's basically storing data in the users table instead of saving it in local array and the passing the user data+other empty columns as json response appended with a long html page. My questions are: 1. How do I just store the json response in local array? 2. How do I pass that array to json view? 3. How do I receive back that array in just json response?

Please help me out. I've been stuck on this forever! thanks :)

2
When using _serialize, do you really still need the view ctp? The Json View class would not need it and can do the same without the view file overhead - see book.cakephp.org/2.0/en/views/json-and-xml-views.html - mark
Even if i don't use serialize, the output is the same...how can I save a json post request in an array? - Anugrah
You misunderstood. I was not saying you should not use it. I was saying if you use it you dont also need the view. The _serialize - as documented - itself suffices. - mark
Oh...but in any case, does deleting the view solve the problem? What do I add in the controller in place of the view? - Anugrah

2 Answers

0
votes

I figured it out with help from the good people above me who led me to the answer:

$data=$this->request->input('json_decode');

This saves the post json request into a variable called $data.

$this->set(array(
                          'data' => $data,
              '_serialize' => array('data')));

As mark pointed out above, you don't need a view in layouts or view/json etc... serialize is enough for that.The above code will send back a json response if you use the standard cakephp format of postin in the url with .json suffixed and making the method name add()

Thank you for the help guys!

-1
votes

If i understood well:

public function add(){
if($this->request->is('post') || $this->request->is('ajax')){
    if ($this->User->save($this->request->data)) {
                 $this->layout='Ajax' ;
                 /* another actions ..... */
                 $this->set(data,$this->request->data) ; // it's no need i think it will be accessible in view!
                 $this->render('jasonView') ;
    }
}
}