0

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

2
  • Defining _serialize as an array has the added benefit of automatically appending a top-level <response> element when using XmlView. If you use a string value for _serialize and XmlView, make sure that your view variable has a single top-level element. Without a single top-level element the Xml will fail to generate. -Probably cause it returns something else(xml) Commented Dec 19, 2013 at 12:30
  • It is also important to call the url with json extension: action.json - otherwise your serialize approach will fail. And you don't leverage the full potential of the rest responsiveness of cake. Commented Dec 19, 2013 at 14:06

2 Answers 2

2

I have no idea why standard set('_serialize, ...) method isn't working properly :(

But I have: Did you add the RequestHandlerComponent in yor AppController?

You don't have to call

  $this->autoRender = false;
  $this->layout = 'ajax';

This as well, the RequestHandler will take care of it.

Sign up to request clarification or add additional context in comments.

1 Comment

I do have RequestHandler, _serialize works in other controllers but not in this one.
1

Check your version of CakePHP. I had this problem with version 2.4.2 and fixed it by upgrading to 2.4.3.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.