I’m trying to pull json data via ajax from a PHP script, but it is not working.
while ($row = mysql_fetch_array($result)) {
$response = array(
'hello' => $row['name']
);
$responses[] = $response;
}
echo json_encode($responses);
Then I use this JavaScript
$('.clickme').click(function() {
$.ajax({
url: 'http://example.com/testFixGet.php?department=1',
dataType: 'json',
data: 'manufacturer=alpine,kicker',
success: function(json) {
alert(json['hello']);
$('.result_new').html(json);
}
});
});
The dialog presents: ‘Undefined’
But, if I actually load the php page the data is json decoded and it looks like this:
[{"hello":"Rand McNally Soft Case for Most 5\" GPS"}]
You’ll notice your JSON payload contains an array with one object element. Try
Also,
getJSON()is much more concise