javascript - json_encode on a PHP array returns a string -
i've searched through site , can't see question quite mine hope isn't copy.
so i've got php script supposed return json array ajax, , want use array generate url.
however, though i'm pretty sure i've got array on php end, when json_encode i'm getting simple string out on other end.
php code:
$n = 10; $all_titles = array(); while($row = mysqli_fetch_array($result)) { $title = trim($row['job title']); if(array_key_exists($title, $all_titles)) { ++$all_titles[$title]; } else { $all_titles[$title] = 1; } } arsort($all_titles); $top_titles = array_slice($all_titles, 0, $n); $title_list = array(); foreach (array_values($top_titles) $key => $val) { array_push($title_list, array_keys($top_titles)[$key]); } echo json_encode($title_list);
these array operations seem working far, based on other tests i've done, i'm pretty sure $title_list array.
here js:
xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { alert("generated url: " + url_gen(xmlhttp.responsetext)); } }
and problem arises:
function url_gen(list) { var url = list[2]; return url; }
i have varied number in list[#] (list[0], list[1], etc.) , each 1 character, meaning list (which passed in onreadystatechange responsetext php function above) string, not json array.
any suggestions?
that's does. returns string. need parse on client side.
alert("generated url: " + url_gen(json.parse(xmlhttp.responsetext)));
Comments
Post a Comment