ajax json decode returning undefined not data

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’, …

via PHP Website Development » Search Results » ajax:

ajax json decode returning undefined not data

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
alert(json[0][‘hello’]);Also, getJSON() is much more concise
$(‘.clickme’).click(function()
$.getJSON(‘http://example.com/testFixGet.php’,
department: 1,
manufacturer: ‘alpine,kicker’
, function(json)
// loop over results
for (var i = 0; i < json.length; i++)
var response = json[i];
console.log(response);

});
});
…………………………………..

Try this:
$(‘.clickme’).click(function()
$.getJSON(‘testFixGet.php’, manufacturer: “alpine,kicker”, department: “1″ , function(json)
alert(json[0].hello);
$(‘.result_new’).html(json);
);
}
…………………………………..

Are you setting the content type?
header(‘Content-type: application/json’);ANd by the way
[“hello”:”Rand McNally Soft Case for Most 5″ GPS”]it is an array so it would be
alert(json[0][“hello”]);To loop
var bar = [“a”:”1″,”a”:”2″,”a”:”3″]
$.each( bar, function(i, jObj)
alert( jObj.a );

);

For more info: ajax json decode returning undefined not data

PHP Website Development » Search Results » ajax

ajax json decode returning undefined not data

Share this post:

Related Posts

Leave a Comment