473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jun 27 '08 #1
1 1988
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tony | last post by:
I've done a fair bit of searching on this, but I want to be certain about what I've found - so here goes with a long example and three questions: For clarity, let me give an example (a number of...
5
by: dougwig | last post by:
I'm trying to handle the scenario where a user's session times out and and their ajax request triggers a redirection by the webserver (302 error?). I'm using Prototype 1.4 and the my works great...
17
by: Steve-O | last post by:
The following code works great in FireFox, Opera, Netscape, Safari, and Gecko, but NOT IE. Why? I tried using 'native' js with setInterval and setTimeout, but I get the same result. My IE...
2
by: Mark Knochen | last post by:
Hi, i have a little problem with ajax. The follow functions are in my site: function sndReq(ID,divID) { resObjekt.open('get','inc/inc_change_pagelogo.php?ID='+ID,true);...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
1
by: satishr23 | last post by:
Hi, I am having some problems with handling cross domain requests in javascript/ajax and would really appreciate any help. My client has a javascript file which he includes in his HTML...
10
by: Piotr Nowak | last post by:
Hi, Say i have a server process which listens for some changes in database. When a change occurs i want to refresh my page in browser by notyfinig it. I do not want to refresh my page i.e....
3
by: john6630 | last post by:
I ran across this ajax framework and it looks very good. It addresses both the JS and PHP sides with a clever "controller" on the PHP side and Firebug debugging on the client side. Also has a nice...
29
by: zalek | last post by:
I am writing application with Ajax in sync mode - xmlHttp.open("GET", url, false). I noticed that in FireFox handler doesn't starts. It starts when I use xmlHttp.open("GET", url,true). I need to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.