Getting Response From Jquery JSON
- by Howdy_McGee
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.