Google Groups Home
Help | Sign in
Message from discussion My ajax class - not handling multiple requests
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
bizt  
View profile
 More options May 16, 12:46 pm
Newsgroups: comp.lang.javascript
From: bizt <bissa...@yahoo.co.uk>
Date: Fri, 16 May 2008 09:46:47 -0700 (PDT)
Local: Fri, May 16 2008 12:46 pm
Subject: My ajax class - not handling multiple requests
Hi,

I am having my first real attempt at an ajax class as so far Ive
managed to build one that, once instatiated, will allow me to define
which function it will call on completion then retrieves the contents
from a server side script as AJAX generally does and process it using
that function. This far Im quite pleased with it, however, I really
want something that will allow me to perform multiple AJAX calls from
the same script. I figured my class would allow this but apparently
not. Im guessing that when one request is in progress the other one is
being attempted and there is a confict somewhere. My debugger doesnt
report any faults but only one of the calls presents any results.
Below is my script (uncomment last two lines to test with multiple
requests):

function AjaxObj() {

  // PRIVATE PROPERTIES
  var _xmlhttp = null;
  var _method;
  var _uri;
  var _readyStateChangeFunction;

  // -----------------------------------------

  // PUBLIC METHODS
  this.setReadyStateChangeFunction = function(func) {
    _setxmlhttp();

    _xmlhttp.onreadystatechange = function() {
      if (_xmlhttp.readyState==4) {// 4 = "loaded"
        if (_xmlhttp.status==200) {// 200 = "OK"
          // now what??
          func();
        } else {
          alert("Problem retrieving XML data:" + _xmlhttp.statusText);
        }
      }
    };
  };

  this.sendRequest = function(uri, method) {
    _method = method;
    _uri = uri;
    _xmlhttp.open(_method, _uri, true);
    _xmlhttp.send(null);
  };

  this.getResponseText = function(func) {
    return _xmlhttp.responseText;
  };

  this.getResponseXML = function(func) {
    return _xmlhttp.responseXML;
  };

  // PRIVATE METHODS
  // Sets XMLHttpRequest object
  function _setxmlhttp() {
    // this cond checks if we have already set xmlhttp, if so return
true
    //if(_xmlhttp) return true;

    // request has not been set, we will generate it here
    _xmlhttp = null;
    if (window.XMLHttpRequest) {// code for IE7, Firefox, Mozilla,
etc.
      _xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {// code for IE5, IE6
      _xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (_xmlhttp==null) {
      alert("Your browser does not support XMLHTTP.");
    }
  }

}

function doSomething() {
  alert(ajax.getResponseText());
}

function doSomethingElse() {
  alert('something else');

}

ajax = new AjaxObj;

ajax.setReadyStateChangeFunction(doSomething);
ajax.sendRequest('data/note.xml', 'GET');

//ajax.setReadyStateChangeFunction(doSomethingElse);
//ajax.sendRequest('data/cd_catalog.xml', 'GET');

At a glance, is there any reason why this might not work?
Does XmlHttpRequest only allow one request at a time? Should I perhaps
implement some kind of queueing system for requests (when one request
is in progress, the others are added to an array - when readystate = 4
the next is run .. if this is possible, not tried it yet)?
Any other suggestions/imporvements as this is my first attempt,
genarally looking for something thats lite and fast?

Any help would be much appreciated. Thanks

Burnsy


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google