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