4

So I have my JavaScript making an Ajax call to /my_controller/ajax_action but then in the controller I don't know what to do to output something back to the JavaScript.

I am getting errors because there is no view for MyController::ajaxAction() but obviously there is no view for it, so what do I do?

4
  • google: sanisoft.com/blog/2010/10/25/… Commented Aug 19, 2011 at 16:40
  • What if the response isn't JSON? I would like a solution that doesn't just return JSON strings, but anything. Commented Aug 19, 2011 at 16:45
  • 2
    well, that is not what the title of the question says ^^ Commented Aug 19, 2011 at 17:08
  • You should always return JSON if you ask for JSON (via .json extension) for example. What else would it be? You can pass pretty much anything with JSON. See this article on details how to achieve that. Commented Jul 10, 2014 at 23:18

4 Answers 4

8

do this, have your variables you want to output in an array let's say $data, then pass that array to the view using the $this->set('data', $data); method, then create a view /General/SerializeJson.ctp. In that view file, put <?PHP echo json_encode($data); ?> after that you can use $this->render('/General/SerializeJson'); and it should output the json.

General code...

/Controllers/MyController.php

public class MyController extends AppController
{
    public function ajaxAction()
    {
        $data = Array(
            "name" => "Saad Imran",
            "age" => 19
        );
        $this->set('data', $data);
        $this->render('/General/SerializeJson/');
    }
}

/Views/General/SerializeJson.ctp

<?PHP echo json_encode($data); ?>
Sign up to request clarification or add additional context in comments.

Comments

4

Easiest way I found was to disable the automatic rendering:

function ajax_action($data = null) {
    if($this->RequestHandler->isAjax()) {
        $this->autoRender = false;
        //process my data and return it
        return $data;
    } else {    
        $this->Session->setFlash(__('Not an AJAX Query', true));
        $this->redirect(array('action' => 'index'));
    }   
}

Comments

3

try this:

within your view folder for the corresponding controller(my_controller) make a folder named json and place a file named index.ctp and within that ctp file write this code:

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

within your my_controller in index() wrote this code:

$this->set('yourVariableNameReturnedFromController', $this->YOURMODEL->find('all'));

within your app_controller.php(if not exist you have to made it) write this code

function beforeFilter(){
  if ($this->RequestHandler->ext == 'json') {
     Configure::write('debug', 0);
  }
}

1 Comment

here i use index() function for accessing the json and your url will like '/my_controller/index.json'
1

AutoRender=false and Return json_encode($code)

public function returningJsonData($estado_id){
    $this->autoRender = false;

    return json_encode($this->ModelBla->find('first',array(
        'conditions'=>array('Bla.bla_child_id'=>$estado_id)
    )));
}

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.