简体   繁体   English

JSON.parse(xmlhttp.responseText)返回空

[英]JSON.parse(xmlhttp.responseText) returns empty

I am using Ajax. 我正在使用Ajax。 My search.php contains the javascript code. 我的search.php包含javascript代码。 It requests for content.php which echoes an array $res that contains values in the form of "key" : value . 它请求content.php ,它回显包含$res "key" : value形式的数组$res "key" : value

content.php : content.php:

echo json_encode($res);

search.php: search.php:

 <!--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);

xmlhttp.send();

</script>

However, when I print $res alone in a separate page, it shows the correct output which is: 但是,当我在单独的页面中单独打印$res时,它显示的正确输出是:

{"1894":1,"1905":0,"1916":0,"1927":0,"1938":0,"1949":0,"1960":0,"1971":0,"1982":0,"1993":0,"2004":1,"2015":2}

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. 我已经尝试了好几天,但都找不到有效的解决方案。

Edit: 编辑:

here is the output of alert(xmlhttp.responseText) : 这是alert(xmlhttp.responseText)的输出:

part 1 of the alert window 警报窗口的第1部分

part 2 of the alert window 警报窗口的第2部分

couldn't post more than two links (because I don't have enough reputation), anyway you had a glimpse of it, I believe. 我认为发布的链接不能超过两个(因为我的信誉不高),无论如何您都可以浏览一下。

According to what I can see in both images, your PHP is giving you HTML code after the JSON object, that could be the problem that stops you from parsing it. 根据我在两张图中看到的图像,您的PHP在JSON对象之后提供了HTML代码,这可能是阻止您解析它的问题。 Make sure you end PHP execution after echoing $res . 确保在回显$res后结束PHP执行。 In other words, try with this line: 换句话说,请尝试以下行:

echo json_encode($res);exit;

Also, in case that doesn't work, please change alert(xmlhttp.responseText) to console.log(xmlhttp.responseText) , check the browser console and copy here all the text you get back from the server 另外,如果无法正常工作,请将alert(xmlhttp.responseText)更改为console.log(xmlhttp.responseText) ,检查浏览器控制台并在此处复制从服务器获取的所有文本

<script type="text/javascript">
    var url=<?php echo "'" . $this->config->base_url() . 'index.php/content.php';?>;
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {/* this was unclosed */
        if( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
            var r=xmlhttp.responseText;
            console.info('ajax response: %s ',r);
            var array = JSON.parse(r);
        }
    }
    xmlhttp.open( "GET", url, true );
    xmlhttp.send();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM