By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

My ajax class - not handling multiple requests

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
bizt meinte:
ajax.setReadyStateChangeFunction(doSomething);
ajax.sendRequest('data/note.xml', 'GET');
Your setting the response listener, trigger the request...
//ajax.setReadyStateChangeFunction(doSomethingElse);
....and overwrite the previously assigned response listener with a new one.
//ajax.sendRequest('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
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.