Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Several parallel request with ajax

58 views
Skip to first unread message

khizh...@gmail.com

unread,
Mar 31, 2007, 7:55:02 AM3/31/07
to
I want to create web page with several parts. There are user
information, menu, news and body parts. And I have a function
loadPage() that create this parts and insert in html.
for example some code
<html>
<head><script src="my.js"></script></head>
<body onload="loadPage()">
<div id="user_info_div"></div>
<div id="menu_div"></div>
<div id="news_div"></div>
<div id="body_div"></div>
</body>
</html>

in my.js file:

var req = (window.ActiveXObject) ? new
ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;

function doRequest(url, fCallback)
{
req.open('GET', url);
req.onreadystatechange = fCallback;
req.send(null);
}

function loadPage()
{
doRequest("/get_user_info.pl", showUserInfo);
doRequest("/get_menu.pl", showMenu);
doRequest("/get_news.pl", showNews);
doRequest("/get_body.pl", showBody);
}
// end of example

But, when this function executes i get an error, because it starts new
request where previous was not finished.

How I make several requests in same time ?

Jacques Jocelyn

unread,
Mar 31, 2007, 8:35:08 AM3/31/07
to dev-...@lists.mozilla.org

Hello khizhaster,

you may want to create your xhmlHttpRequest only when needed, and not
when the script is loading. i.e

function createRequest(){
....


var req = (window.ActiveXObject) ? new
ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;

...
return(XMLHttpRequest)
}


function doRequest(url, fCallback) {
var req = null
req = createRequest();
if(req == null) {
....
alert ("Error wile creating REQ ")
}
else{


req.open('GET', url);
req.onreadystatechange = fCallback;
req.send(null);
}
}

you may also look at examples available on the net
http://www.google.com/search?num=30&hl=en&newwindow=1&safe=off&q=xmlhttprequest+concurrent&btnG=Search

Best Regards
Jacques Jocelyn

Martin Honnen

unread,
Mar 31, 2007, 10:09:01 AM3/31/07
to
khizh...@gmail.com wrote:

> var req = (window.ActiveXObject) ? new
> ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;

That way with IE 7 your code would create and use the ActiveXObject
instead of using XMLHttpRequest. Consider changing that to e.g.
var req = null;
if (typeof XMLHttpRequest != 'undefined') {
req = new XMLHttpRequest();
}
else if (typeof ActiveXObject != 'undefined') {
try {
req = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
try {
req = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e2) {}
}
}

> But, when this function executes i get an error, because it starts new
> request where previous was not finished.
>
> How I make several requests in same time ?

You need to create one request object per request, for instance by
moving the creation code (e.g. new XMLHttpRequest()) into the doRequest
function.
On the other hand there are recommended limits on the number of
simultaneous requests a HTTP user agent is supposed to make so don't
expect that the browser makes lots of HTTP requests at the same time.


--

Martin Honnen
http://JavaScript.FAQTs.com/

khizh...@gmail.com

unread,
Apr 1, 2007, 11:04:18 AM4/1/07
to
*Jacques Jocelyn*, thanx for examples, but... there is a one problem.
For example, I have callback function like this:

function showMenu()
{
if (req.readyState == 4 && req.status == 200)
{
// do something with menu if state is ok
}
}

If I was created a request in function like doRequest() I cann't get
Request object in callback function. For example, in function above
object 'req' is not defined and I cann't check status of request. Is
it possible to get request object that

*Martin Honnen*
Hmmm.. I have a Vista intaslled and check my code in Firefox 2.x and
IE 7. All work good. But all the same thanks :)

P.S. Sorry for my english, i'm not englishman. :(

khizh...@gmail.com

unread,
Apr 1, 2007, 11:06:24 AM4/1/07
to
> Is it possible to get request object that

Is it possible to get request objectthat relates to this callback
function?

Martin Honnen

unread,
Apr 1, 2007, 12:15:44 PM4/1/07
to
khizh...@gmail.com wrote:

> Hmmm.. I have a Vista intaslled and check my code in Firefox 2.x and
> IE 7. All work good.

I did not say that the code does not work. However if you first check
for ActiveXObject then you don't use XMLHttpRequest with IE 7 although
it does support it and is supposed to improve performance. See
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_xmlhttprequest.asp>

khizh...@gmail.com

unread,
Apr 2, 2007, 4:03:27 AM4/2/07
to
Thanks for links, i have found all what I need :)

0 new messages