I'm trying to get an array of object from php with ajax request.
Until now all is ok, i get my array in json format in my ajax response in javascript {"ID:"9348","Name":"Mark"}
.
But now if i try to get the object with alert(data["ID"])
$.ajax({ url:'loaddata.php', dataType:'json', success:function(data){ alert(data['ID']); }, error: function (thrownError) { alert("errore"); } });
i get " undefined
" Can anybody help me ???
in my php file i have this:
$a=array(); while($row = mysql_fetch_array($result,MYSQL_ASSOC)){ $a[] = json_encode (array('ID'=>$row['ID'],'name'=>$row['name'])); } header("Content-Type: application/json"); echo json_encode($a);
From the php code you posted it seems that you return an array. therefor you have to select the dataset by array index:
alert(data[0]['ID']);
and your json probably looks like this:
[ {"ID":"9348","Name":"Mark"}, ... ]
you are double encoding
in-loop: $a[] = json_encode (... // this makes $a an array of json-strings
outside: echo json_encode($a ... // this echos an JSON representation of an array of json-strings
this will not work, just produce/gather your array, then at the end do json_encode
once
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.