CodeIgniter Forums
getJSON not working correctly with codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: getJSON not working correctly with codeigniter (/thread-57490.html)



getJSON not working correctly with codeigniter - El Forum - 03-18-2013

[eluser]Unknown[/eluser]

I am trying to implement AJAX call method into my website using codeigniter, the reason why i want to use AJAX so i can get live updates from the database
The click button works and displays all of the JSON data but the issue is when i try and display a specific array it does not recognize it, it seems to be iterating through every single character.

Things i have done to try and work it out

i have alerted the datatype and found out it was a string

i have managed to get ajax to work in regular PHP rather then codeigniter so their is nothing wrong with the database

I have also tried to use jQuery.parseJSON so i can force it to be json but then the script did not run

I used JSONlint and apprently its a valid JSON

Consolelog

Code:
[
[
    {
        "id": "1",
        "postcode": "NW6",
        "imgurl": "hello.jpeg",
        "from_user": "henny",
        "created_at": "2013-03-18 23:03:00"
    },
    {
        "id": "2",
        "postcode": "NW2",
        "imgurl": "test.jpeg",
        "from_user": "belly",
        "created_at": "2013-03-15 23:03:00"
    }
]
]

Controller

Code:
public function insertJSON()
{
    $this->load->model("values");
    $queryresults = $this->values->getDb();

    $arrays = array($queryresults);

    echo json_encode($arrays);  
}

View

Code:
[removed]

  $('#getdata').click(function (data) {

      $.ajax({

          url: '<?php echo base_url().'index.php/welcome/insertJSON';?>',
          type: 'POST',
          cache: 'false',
          datatype: 'json'

      }).done(function (data) {

         alert(data);

      });

  });

[removed]
json_encode

Code:
[
[
    {
        "id": "1",
        "postcode": "NW6",
        "imgurl": "hello.jpeg",
        "from_user": "henny",
        "created_at": "2013-03-18 23:03:00"
    },
    {
        "id": "2",
        "postcode": "NW2",
        "imgurl": "test.jpeg",
        "from_user": "belly",
        "created_at": "2013-03-15 23:03:00"
    }
]
]

Any help would be appreciated


getJSON not working correctly with codeigniter - El Forum - 03-18-2013

[eluser]CroNiX[/eluser]
I don't see you sending json headers before you output the json.

I put this in a helper function...

Code:
function send_json($array)
{
  header('Cache-Control: no-cache, must-revalidate');
  header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  header('Content-type: application/json');
  echo json_encode($array);
  exit();
}



getJSON not working correctly with codeigniter - El Forum - 03-18-2013

[eluser]CroNiX[/eluser]
Also, I think you have an extra array layer in your json.

$queryresults already looks like it's an array, so this is probably also causing a problem as you wrap it all in another array:
Code:
$arrays = array($queryresults);

Which is probably why you have a double set of brackets at that start and end ([[) and (]]) of your json output.