I am trying to retrieve array that was created by php and send back to my JS script via ajax.
I am not sure how to display the value.
server side php
$r=array();
$r[] = 'aaa';
$r[] = 'bbb';
$r[] = 'ccc';
echo json_encode($r);
my JS
....ajax codes....
var p=document.getElementById('text');
if (xmlhttp.readyState==4 && xmlhttp.status==200){
var r=xmlhttp.responseText;
for (var i=0; i<r.length; i++){
p.innerHTML= r[i] + '<br>';
}
}
The output will be
a
a
a
b
b
b
c
c
c
//but I want these
aaa
bbb
bbb
I want to use javascript instead of $.ajax to complete this. Any ideas?? Thanks a lot.
JSON.parse()the response you get inxmlhttp.responseText, it will not magically decode JSON by itself as jQuery does.