Getting Response From Jquery JSON

Posted by Howdy_McGee on Stack Overflow See other posts from Stack Overflow or by Howdy_McGee
Published on 2012-04-08T07:37:57Z Indexed on 2012/04/08 11:29 UTC
Read the original article Hit count: 154

Filed under:
|
|
|
|

I'm having trouble getting a response from my php jquery / json / ajax. I keep combining all these different tutorials together but I still can't seem to pull it all together since no one tutorial follow what I'm trying to do.

Right now I'm trying to pass two arrays (since there's no easy way to pass associative arrays) to my jquery ajax function and just alert it out. Here's my code:

PHP

$names = array('john doe', 'jane doe');
$ids = array('123', '223');

$data['names'] = $names;
$data['ids'] = $ids;

echo json_encode($data);

Jquery

function getList(){
    $.ajax({  
        type: "GET", 
        url: 'test.php', 
        data: "",  
        complete: function(data){ 
            var test = jQuery.parseJSON(data);
            alert(test.names[0]);
            alert("here");
        }
    },
        "json");
}
getList();

In my html file all I'm really calling is my javascript file for debugging purposes. I know i'm returning an object but I'm getting an error with null values in my names section, and i'm not sure why. What am I missing?

My PHP file returns

{"names":["john doe","jane doe"],"ids":["123","223"]}

It seems to be just ending here Uncaught TypeError: Cannot read property '0' of undefined so my sub0 is killing me.

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript