动态Ajax请求仅显示最终响应文本

时间:2013-03-12 18:58:39

标签: php ajax responsetext

我在下面看到了类似的问题: 2 AJAX commands that work, but I only see the last "responseText"

但我使用的是一系列字段,所以我不确定这是否会使它与众不同。 为了进行测试,我将其添加到顶部以定义一些数据:

<?php
    $a = array('star_wars','marvel','board_games','','The_Han_Solo_Omnibus');
    $b = implode("~",$a);
    $count = count($a);
?>

我在上面的答案中看到他定义了:var httpRequest;解决问题。 我试过 - var xmlhttp;但那没有做到。

关于如何加载所有部分的任何想法,而不仅仅是最后一部分?我甚至添加了大量的延迟,但没有任何工作。

感谢

<script>

function showDiv(a)
{
var temp = new Array();
var delay = 2000;            // 1 second
var result = 'loading';    // the result from your AJAX response
var xmlhttp;

        temp = a.split('~');
        for(i=0;i<temp.length;i++)
        {
            alert(temp[i]); 
                divno =  i + 1;
            currentdiv = "loadArea" + divno;
            alert (currentdiv);

                    //var pageNumber = 1;
                    //eval("var text" + pageNumber + "=123;");
                    //alert(text1);

            if (temp[i]=="")
                  {
                  document.getElementById(currentdiv).innerHTML=""; //increment
                  }

            if (window.XMLHttpRequest)
                    {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();//increment
                    alert ("usingchrome");
                    }
            else
                    {// code for IE6, IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");//increment
                    //alert ("using IE");
                    }

            xmlhttp.onreadystatechange=setTimeout(function()  //increment
                    {
                        alert ("inreadystate");
                         if (xmlhttp.readyState==4 && xmlhttp.status==200) //increment
                                {
                                if (temp[i] !="") {
                                setTimeout(function() {     
                                document.getElementById(currentdiv).innerHTML=xmlhttp.responseText; //increment                         

                                                    }, delay);



                                alert ('valueisgood');
                                                 }

                                }
                      }, delay);


                                if (temp[i] !="") {
                                    url = "http://zocreative.com/load.php?f="+temp[i];
                                    xmlhttp.open("GET",url,true);
                                    xmlhttp.send();
                                }

                  } 
        }





</script>
</head>
<body id="page1" onload="showDiv('<?php echo $b; ?>');">

<?php

$i = 1;
while ($i <= $count) {

    echo ("<div id='loadArea". $i ."'><br />
    <!-- AJAX content will load here-->
    loading ... ". $i ."
    </div>");
      /* the printed value would be
                   $i before the increment
                   (post-increment) */
    $i++;
}
?>
<div id="note"></div>

1 个答案:

答案 0 :(得分:0)

我强烈建议您查看jQuery.load:http://api.jquery.com/load/。你要做的事情并不像你写的代码那么复杂。

我不打算在这里保证确切的语法,但是这里简化了上面使用jQuery和jQuery加载重写的Javascript代码:

function showDiv(a) {
var temp = a.split('~'),
    divno;

for(i = 0; i < temp.length; i += 1) {
    divno =  i + 1;
    jQuery('#loadArea' + divno.toString()).load('http://zocreative.com/load.php?f=' + temp[i]);
}

这里的关键是你要求jQuery将URL加载到指定的div而不必自己跟踪异步响应。 jQuery.load方法有几个选项可以让你响应完成,所以请阅读文档。