Consecutive requests with XMLHttpRequest under IE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robloche

    Consecutive requests with XMLHttpRequest under IE

    Hi,

    I wrote a class to handle requests from the client-side. In particular,
    I use :

    fuction myClass() {
    // ...
    this.xhr_object = new ActiveXObject(" Microsoft.XMLHT TP");
    // ...
    }

    In the first implementation of this class, I only instantiated
    this.xhr_object (as shown above) once, when a "myClass" object was
    created. But then, it happened that I couldn't perform more than one
    request with this object : from the second request, the function
    onreadystatecha nge would never be called (I made many tests to be sure
    of that).

    So I changed my class and now I create a new object (with
    this.xhr_object = new ActiveXObject(" Microsoft.XMLHT TP");) every time a
    request is made. And it works.

    So my question is :
    Is this behavior normal or have I miss something ?

    (In fact, my class is a wrapper for IE and Firefox and under Firefox
    this problem doesn't exist : Once you have your HTMLHttpRequest object,
    you can make as many requests as you want.)
    Thank you all for your (future) answers !

  • Martin Honnen

    #2
    Re: Consecutive requests with XMLHttpRequest under IE



    Robloche wrote:

    [color=blue]
    > I wrote a class to handle requests from the client-side. In particular,
    > I use :
    >
    > fuction myClass() {
    > // ...
    > this.xhr_object = new ActiveXObject(" Microsoft.XMLHT TP");
    > // ...
    > }
    >
    > In the first implementation of this class, I only instantiated
    > this.xhr_object (as shown above) once, when a "myClass" object was
    > created. But then, it happened that I couldn't perform more than one
    > request with this object : from the second request, the function
    > onreadystatecha nge would never be called (I made many tests to be sure
    > of that).[/color]

    No problem here with IE 6 and the following:

    var httpRequest = new ActiveXObject(' Microsoft.XMLHT TP');

    for (var i = 0; i < 2; i++) {
    httpRequest.ope n('GET', location.href, true);
    httpRequest.onr eadystatechange = function () {
    if (httpRequest.re adyState == 4) {
    alert(httpReque st.responseText );
    };
    };
    httpRequest.sen d(null);
    }

    the alert shows up twice.

    I suspect you set the onreadystatecha nge handler only once but do
    several open/send calls then, I think that is a problem with older
    versions of MSXML. So it shouldn't be necessary to create a new object
    each time you want to make a request but to ensure that after each open
    call the onreadystatecha nge handler is set.


    --

    Martin Honnen

    Comment

    • Robloche

      #3
      Re: Consecutive requests with XMLHttpRequest under IE

      First of all, thank you very much for your answer.
      I tried your little piece of code and, of course, it worked.
      But as soon as I put it in a class, the bug occurs. Here's my code :

      function CreateXMLHTTPRe questObject() {
      this.xhr_object = new ActiveXObject(" Microsoft.XMLHT TP");

      this.doRequest = function(url) {
      //this.xhr_object = new ActiveXObject(" Microsoft.XMLHT TP");

      var obj = this;
      this.xhr_object .onreadystatech ange = function() {
      alert("onreadys tatechange\nrea dyState :
      "+obj.xhr_objec t.readyState);
      if(obj.xhr_obje ct.readyState == 4)
      alert(obj.xhr_o bject.responseT ext);
      }

      this.xhr_object .open('GET', url, true);
      this.xhr_object .send(null);
      }
      }

      var req = new CreateXMLHTTPRe questObject();
      req.doRequest(l ocation.href);
      req.doRequest(l ocation.href);



      As it is, only the first request succeeds. In the second one, the first
      alert in the onreadystatecha nge function never shows up...

      But if I uncomment the first line of the function doRequest(url), then
      the two requests succeed and all the alerts show up.
      What's wrong with my class ?

      Comment

      • Martin Honnen

        #4
        Re: Consecutive requests with XMLHttpRequest under IE



        Robloche wrote:

        [color=blue]
        > But as soon as I put it in a class, the bug occurs. Here's my code :
        >
        > function CreateXMLHTTPRe questObject() {
        > this.xhr_object = new ActiveXObject(" Microsoft.XMLHT TP");
        >
        > this.doRequest = function(url) {
        > //this.xhr_object = new ActiveXObject(" Microsoft.XMLHT TP");
        >
        > var obj = this;
        > this.xhr_object .onreadystatech ange = function() {
        > alert("onreadys tatechange\nrea dyState :
        > "+obj.xhr_objec t.readyState);
        > if(obj.xhr_obje ct.readyState == 4)
        > alert(obj.xhr_o bject.responseT ext);
        > }
        >
        > this.xhr_object .open('GET', url, true);
        > this.xhr_object .send(null);[/color]

        I would change the order as follows (first open(), then setting
        onreadystatecha nge, then send()):

        this.xhr_object .open('GET', url, true);

        var obj = this;
        this.xhr_object .onreadystatech ange = function() {
        alert("onreadys tatechange\nrea dyState : "+obj.xhr_objec t.readyState);
        if(obj.xhr_obje ct.readyState == 4)
        alert(obj.xhr_o bject.responseT ext);
        }

        this.xhr_object .send(null);


        Then it works here for me with IE 6 for both requests.


        --

        Martin Honnen

        Comment

        • Robloche

          #5
          Re: Consecutive requests with XMLHttpRequest under IE

          Thank you !!!
          It was so simple... I feel kind of dumb... :o/

          But why does it work only once when the onreadystatecha nge is set
          before opening ?
          I went through many web pages that deal with this and I found either
          this order or the other one.
          Anyway, you saved me hours of debugging, so thanks again !

          Comment

          Working...