I'm trying to read the data sent back to my ajax request from the server. I echo back an array then I want to read each value and place it on the desired place on the page. I'm not sure how to work with the JSON that was sent back. The example I looked at seemed to say I just needed to reference it like an array, but that doesn't work.
//AJAX Request
$.post("getData.php", {trackingNum: trackNum},
function(result) {
alert(result);
usrID(result[0]);
setTracking(result[1]);
carType(result[2]);
status(result[3]);
});
//PHP
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
$array[0] = $row[0];
$array[1] = $row[1];
$array[2] = $row[2];
$array[3] = $row[3];
}
echo json_encode($array);
What I'm getting back from the alert looks like this: ["2","D78A19C","Nissan","Sanding"] But it won't reference like an array. Help?
$array['username'] = $row[..]
and then in your js code you could use it that way:result.username
(object). Which is much more semantically correct thanresult[0]
– Ofir Baruch May 22 '13 at 13:59