I have problem with JSON. Can someone tell me why first option works and second no? Second option is working in every other controller, but not in this one.
In database i have table with name statuses, and model in CakePHP - Status Is this somehow relevant to this case?
Here is the error i'm recieving
parsererror SyntaxError: JSON.parse: unexpected end of data
My CakePHP controller function option 1 (working):
public function admin_add(){
if($this->request->is('ajax'))
{
$this->autoRender = false;
$this->layout = 'ajax';
$this->Status->create();
if ($this->Status->save($this->request->data)){
$statuses = $this->Status->findById($this->Status->id);
$message = "Status has been added";
$status = "OK";
//$this->set(compact("message",'status','statuses'));
//$this->set('_serialize',array('message','status','statuses'));
echo json_encode(array(
'statuses' => $statuses,
'message' =>$message,
'status' => $status
));
}
}
}
And second NOT working: public function admin_add(){
if($this->request->is('ajax'))
{
$this->autoRender = false;
$this->layout = 'ajax';
$this->Status->create();
if ($this->Status->save($this->request->data)){
$statuses = $this->Status->findById($this->Status->id);
$message = "Status has been added";
$status = "OK";
$this->set(compact("message",'status','statuses'));
$this->set('_serialize',array('message','status','statuses'));
/*echo json_encode(array(
'statuses' => $statuses,
'message' =>$message,
'status' => $status
));*/
}
}
}
And my ajax call:
$.ajax({
type: "post",
url : location.pathname+"/add",
data: data,
dataType: "json",
success : function(resp){ //some actions here }
I have no idea why standard set('_serialize, ...) method isn't working properly :(