0

I want to fill $data array with some values (index action) and to use that array (sender action) to create a JSON object. I want to use that JSON object in index.ctp (jQuery). I tried using the code below but it's not working, I don't receive any data (jsonArr). I think the problem should be on PostsController but I just can't find a solution.

PostsController:

<?php 

class PostsController extends AppController {

    public $data = array();

    public function index()
    {

         $this->layout = 'default';

        this->data[] = array
        (
            array(
                "id" => "1",
                "title" => "title1",
                "author" => "author1"
            ),
            array(
                "id" => "2",
                "title" => "title2",
                "author" => "author2"
            ),
            array(
                "id" => "3",
                "title" => "title3",
                "author" => "author3"
            )

        );
    }

    public function sender()
    {
         $this->layout = 'default';

        foreach ($this->data as $i) {
          foreach ($i as $item) {
               $all[] = array(
                "id" => $item['id'],
                "title" => $item['title'],
                "author" => $item['author']
                );
            }
        }

        $this->set("json",json_encode($all));



    }

}

?>

index.ctp

<div>
Content  
</div>
<div>
Content
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript"> 

var jsonArr = [];

$.getJSON(plgFcRoot + "posts/sender", function(data) {
                  $.each(data, function(key, val) {
                          jsonArr.push({
                            id : val.id,
                            title : val.title,
                            author: val.author
                          })
                  });


 });

    console.log(jsonArr);


</script>

sender.ctp

<?php

echo $json;


?>
halfer
  • 19,824
  • 17
  • 99
  • 186
cosneu
  • 26
  • 4
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – adeneo Feb 05 '14 at 21:11
  • Would you amplify "not working"? It's hard to work out what is going wrong here without more detail. What debugging have you done thus far? – halfer Feb 05 '14 at 21:16
  • in sender.ctp i tested the json object --
      
    -- and its working, if i move all the $data array assignments from index action to the other action --sender-- its working --console.log(jsonArr) show me the objects--
    – cosneu Feb 05 '14 at 21:20
  • all i am looking for is for a solution where the array it is being filed with data from the index action – cosneu Feb 05 '14 at 21:21
  • Really! You're console logging the array outside the callback for the ***ASYNCHRONOUS*** ajax function. Read the link I posted in the first comment ? – adeneo Feb 05 '14 at 21:23
  • Thanks adeneo for the advice, i`ll read it – cosneu Feb 05 '14 at 21:25

0 Answers0