2015-12-04 17:25
浏览 583


I am using Ajax. My search.php contains the javascript code. It requests for content.php which echoes an array $res that contains values in the form of "key" : value.

content.php :

echo json_encode($res);


 <!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"> </script>
 <script type="text/javascript">

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

alert(xmlhttp.responseText); //this alerts the correct content of $res but along with all the HTML page codes
var array = JSON.parse(xmlhttp.responseText); //also tried json_decode(xmlhttp.responseText, true); and jQuery.parseJSON( xmlhttp.responseText );
alert(array); // this doesn't alert at all
xmlhttp.open("GET", "<?php echo   @$this->config->base_url();  ?>index.php/content.php", true);



However, when I print $res alone in a separate page, it shows the correct output which is:


I tried to loop through the array:

var array = JSON.parse(xmlhttp.responseText);

for(var index in array) {

 alert(index+ " is: "+ array[index]);

But this also doesn't alert anything. I've been trying and searching on this for days but couldn't find a working solution.


here is the output of alert(xmlhttp.responseText) :

part 1 of the alert window

part 2 of the alert window

couldn't post more than two links (because I don't have enough reputation), anyway you had a glimpse of it, I believe.

2条回答 默认 最新

相关推荐 更多相似问题