ghi
ghi

Reputation: 725

Run one ajax after another one

I'm working on the project where I (sadly) cannot use jQuery. And I need to do something which is simple in jQuery but I cannot do it in pure JavaScript. So, I need to run one ajax request using a response form another one. In jQuery it will look like:

$.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));
  });

});

And this is my code in pure JS which isn't working:

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

What am I doing worng?

Upvotes: 0

Views: 501

Answers (3)

u.k
u.k

Reputation: 3091

How about something like this (naive prototype):

// 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);
  });
});

Upvotes: 1

MaxArt
MaxArt

Reputation: 22617

It's not clear what you got wrong (can you tell us?), but I'd suggest to rely on some helper function like this:

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;
}

So all you have to do is to use this function:

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

Upvotes: 0

Engineer
Engineer

Reputation: 48793

You forgot to call ObiektXMLHttp.send(null); on second case:

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

Upvotes: 1

Related Questions