My ajax class - not handling multiple requests

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

    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 _readyStateChan geFunction;

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

    // PUBLIC METHODS
    this.setReadySt ateChangeFuncti on = function(func) {
    _setxmlhttp();

    _xmlhttp.onread ystatechange = function() {
    if (_xmlhttp.ready State==4) {// 4 = "loaded"
    if (_xmlhttp.statu s==200) {// 200 = "OK"
    // now what??
    func();
    } else {
    alert("Problem retrieving XML data:" + _xmlhttp.status Text);
    }
    }
    };
    };

    this.sendReques t = function(uri, method) {
    _method = method;
    _uri = uri;
    _xmlhttp.open(_ method, _uri, true);
    _xmlhttp.send(n ull);
    };

    this.getRespons eText = function(func) {
    return _xmlhttp.respon seText;
    };

    this.getRespons eXML = function(func) {
    return _xmlhttp.respon seXML;
    };

    // 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.XMLHttp Request) {// code for IE7, Firefox, Mozilla,
    etc.
    _xmlhttp = new XMLHttpRequest( );
    } else if (window.ActiveX Object) {// code for IE5, IE6
    _xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
    }
    if (_xmlhttp==null ) {
    alert("Your browser does not support XMLHTTP.");
    }
    }

    }




    function doSomething() {
    alert(ajax.getR esponseText());
    }
    function doSomethingElse () {
    alert('somethin g else');
    }

    ajax = new AjaxObj;

    ajax.setReadySt ateChangeFuncti on(doSomething) ;
    ajax.sendReques t('data/note.xml', 'GET');

    //ajax.setReadySt ateChangeFuncti on(doSomethingE lse);
    //ajax.sendReques t('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
  • Gregor Kofler

    #2
    Re: My ajax class - not handling multiple requests

    bizt meinte:
    ajax.setReadySt ateChangeFuncti on(doSomething) ;
    ajax.sendReques t('data/note.xml', 'GET');
    Your setting the response listener, trigger the request...
    //ajax.setReadySt ateChangeFuncti on(doSomethingE lse);
    ....and overwrite the previously assigned response listener with a new one.
    //ajax.sendReques t('data/cd_catalog.xml' , 'GET');
    Does XmlHttpRequest only allow one request at a time?
    An instance of the XmlHttpRequest-Object can handle one request at a
    time. However, you can have plenty of instances.
    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)?
    Yes.
    Any other suggestions/imporvements as this is my first attempt,
    genarally looking for something thats lite and fast?
    So far I haven't needed a queue. Aborting a previous request and
    starting a new one gets the job done - for my applications. In this case
    [1] might be interesting.

    Gregor



    [1] <http://www.quirksmode. org/blog/archives/2005/09/xmlhttp_notes_a _1.html>

    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum

    Comment

    Working...