I have used Mongodb with Php I want to retrive document from mongo collections with ajax call, so I have tried follwing way but i am strugle with access json object in jquery.. Jquery CODE
$('select[name=trendsName]').change(function(){
$.ajax({
url:'getTrendsFullInfo.php',
type: 'POST',
dataType: 'json',
data: {trendsName: $(this).val(), collectionName:'trends_collection'},
success: function(res){
for(var i = 0; i < res.trendsArr['LSM'].length; i++){
$('.resArea').append(res.trendsArr['LSM'][i]);
}
}
});
});
getTrendsFullInfo.php
$finalResultArr = array();
if(isset($_REQUEST['trendsName']) && isset($_REQUEST['collectionName'])):
$status = "Sucessfully Retrived From ".$_REQUEST['collectionName']." Record";
$finalResultArr['LSM'] = array(
'10-12' => 237,
'13-15' => 565,
'16-18' => 825);
$outputArr = array('status'=>$status, 'trendsArr'=>$finalResultArr);
echo json_encode($outputArr);
endif;
How to access trendsArr object in jquery?
for(var i = 0; i < res.trendsArr['LSM'].length; i++){and then use the debugger to look at the data returned from PHP in theresvariableconsole.log(res.trendsArr)chk this first,console.write(res);before theforloop and post what you see.$finalResultArr? It should be initialised as$finalResultArr = [];before usage to get a valid result.