0

我正在从事我(遗憾地)无法使用 jQuery 的项目。我需要做一些在 jQuery 中很简单的事情,但我不能用纯 JavaScript 来做。因此,我需要使用另一个响应表单来运行一个 ajax 请求。在 jQuery 中,它看起来像:

$.get("date.php", "", function(data) {
  var date=data;
  $("#date").load("doku.php?id="+date.replace(" ", "_")+" #to_display", function() {
    $(document.createElement("strong")).html("<a href=\"doku.php?id="+date.replace(" ", "_")+"\">"+date+"</a>:").prependTo($(this));
  });

});

这是我在纯 JS 中的代码,它不起作用:

  if (window.XMLHttpRequest) 
  { 
     ObiektXMLHttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) 
  { 
     ObiektXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } 

  if(ObiektXMLHttp) 
  {
    ObiektXMLHttp.open("GET", "date.php");

    ObiektXMLHttp.onreadystatechange = function() 
    {

      if (ObiektXMLHttp.readyState == 4)
      {
        var date = ObiektXMLHttp.responseText;
        if (window.XMLHttpRequest) 
        { 
           ObiektXMLHttp = new XMLHttpRequest();
        } else if (window.ActiveXObject) 
        { 
           ObiektXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } 

        ObiektXMLHttp.open("GET", "doku.php?id="+date.replace(" ", "_"));
        ObiektXMLHttp.onreadystatechange = function() 
        {
          if (ObiektXMLHttp.readyState == 4)
          {
            alert(ObiektXMLHttp.responseText);
          }
        }
      } 
   } 
   ObiektXMLHttp.send(null);
  }

我在做什么?

4

3 回答 3

1

你忘了打电话ObiektXMLHttp.send(null);给第二种情况:

//....
ObiektXMLHttp.open("GET", "doku.php?id="+date.replace(" ", "_"));
ObiektXMLHttp.onreadystatechange = function() {
   if (ObiektXMLHttp.readyState == 4)
   {
      alert(ObiektXMLHttp.responseText);
   }
};
//Here
ObiektXMLHttp.send(null);
于 2012-05-18T21:15:55.363 回答
1

像这样的东西怎么样(天真的原型):

// xhr object def
var xhr = {
  obj: function() {
    if (window.XMLHttpRequest) {
      return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      return new ActiveXObject("Microsoft.XMLHTTP");
    }
    throw new Error("can't init xhr object");
  },
  get: function(url, fn) {
    var xhr = this.obj();
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {
        fn(xhr.responseText);
      } 
    };
    xhr.open("GET", url);
    xhr.send(null);
  }
};

// implementation
xhr.get("date.php", function(data){
  xhr.get("doku.php?id=" + data.replace(" ", "_"), function(data){
    alert(data);
  });
});
于 2012-05-18T21:19:37.093 回答
0

目前还不清楚你做错了什么(你能告诉我们吗?),但我建议依赖一些这样的辅助函数:

function xhrGet(url, callback) {
    if (window.XMLHttpRequest)
        var xhr = new XMLHttpRequest();
    else if (window.ActiveXObject)
        var xhr = new ActiveXObject("Microsoft.XMLHTTP");
    if (!xhr) return;
    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        if (xhr.readyState !== 4) return;
        if (typeof callback === "function") callback(xhr);
    };
    xhr.send(null);
    return xhr;
}

所以你所要做的就是使用这个函数:

xhrGet("date.php", function(x1) {
    xhrGet("doku.php?id=" + date.replace(" ", "_"), function(x2) {
        // do stuff
        // x1 and x2 are respectively the XHR object of the two requests
    });
});
于 2012-05-18T21:16:21.557 回答