Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 一个接一个的ajax请求_Javascript_Ajax - Fatal编程技术网

Javascript 一个接一个的ajax请求

Javascript 一个接一个的ajax请求,javascript,ajax,Javascript,Ajax,我需要一个功能,点击一个链接,发送ajax请求到2页一个接一个 首先将数据发送到page1.php,完成page1的工作后,将数据返回主页面,然后将数据发送到page2.php执行另一个ajax进程并将数据返回主页面 我在page1.phpdocument.getElementById(“div1”).innerHTML=HttPRequest.responseText中编写page2.php请求,但这只会返回page2的数据,并从page1.php 在firebug中,请咨询: page1.p

我需要一个功能,点击一个链接,发送ajax请求到2页一个接一个

首先将数据发送到
page1.php
,完成page1的工作后,将数据返回主页面,然后将数据发送到
page2.php
执行另一个ajax进程并将数据返回主页面

我在
page1.php
document.getElementById(“div1”).innerHTML=HttPRequest.responseText中编写
page2.php
请求
,但这只会返回
page2的
数据,并从
page1.php

在firebug中,请咨询:

page1.php (always show loading.gif)
page2.php 200 OK 2199ms
如何逐一做好ajax?谢谢

function myajax(text) {
      var HttPRequest = false;
      if (window.XMLHttpRequest) {
         HttPRequest = new XMLHttpRequest();
         if (HttPRequest.overrideMimeType) {
            HttPRequest.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) {
         try {
            HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      } 
      if (!HttPRequest) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
        var url = 'page1.php';
        var pmeters = "text=" + text;
        HttPRequest.open('POST',url,true);
        HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        HttPRequest.send(pmeters);
        HttPRequest.onreadystatechange = function()
        {

             if(HttPRequest.readyState == 4) 
              {
               document.getElementById("div1").innerHTML = HttPRequest.responseText;

               var url = 'page2.php';
                    var pmeters = "text=" + text;
                    HttPRequest.open('POST',url,true);
                    HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    HttPRequest.send(pmeters);
                    HttPRequest.onreadystatechange = function()
                    {

                         if(HttPRequest.readyState == 4) 
                          {
                           document.getElementById("div2").innerHTML = HttPRequest.responseText;
                          }
                    }
              }
        }
   }

有两种选择

  • 在同步模式下使用Ajax,因此代码将等待您从第1页获得结果
  • 仅在Page1的响应处调用Page2的Ajax方法
  • 选项2更好,因为它在异步模式下工作

    下面是一个伪代码:

     var xhr = new XMLHttpRequest();
     xhr.open( ..)
     xhr.onreadstatechange = function() {
          // handle page1 response
    
          // do page2 reauest
          var xhr2 = new XMLHttpRequest();
          xhr2.open( ... )
          xhr2.onreadystatechange =  function() {
            // handle page2 response
    
          }
          xhr2.send(null);
     }
     xhr.send(null);
    

    我建议使用jqueryforajax这样的框架。