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.
You have to parse the json string before, e.g. with JSON.parse. Try (untested):
=== UPDATE ===
If you have to support very old browsers (e.g. less or equal then IE7) you should use libraries like Crockfords JSON2parser or jQuery (a huge lib with much more features).