473,473 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Consecutive requests with XMLHttpRequest under IE

Hi,

I wrote a class to handle requests from the client-side. In particular,
I use :

fuction myClass() {
// ...
this.xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
// ...
}

In the first implementation of this class, I only instantiated
this.xhr_object (as shown above) once, when a "myClass" object was
created. But then, it happened that I couldn't perform more than one
request with this object : from the second request, the function
onreadystatechange would never be called (I made many tests to be sure
of that).

So I changed my class and now I create a new object (with
this.xhr_object = new ActiveXObject("Microsoft.XMLHTTP");) every time a
request is made. And it works.

So my question is :
Is this behavior normal or have I miss something ?

(In fact, my class is a wrapper for IE and Firefox and under Firefox
this problem doesn't exist : Once you have your HTMLHttpRequest object,
you can make as many requests as you want.)
Thank you all for your (future) answers !

Jul 23 '05 #1
4 2270


Robloche wrote:

I wrote a class to handle requests from the client-side. In particular,
I use :

fuction myClass() {
// ...
this.xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
// ...
}

In the first implementation of this class, I only instantiated
this.xhr_object (as shown above) once, when a "myClass" object was
created. But then, it happened that I couldn't perform more than one
request with this object : from the second request, the function
onreadystatechange would never be called (I made many tests to be sure
of that).


No problem here with IE 6 and the following:

var httpRequest = new ActiveXObject('Microsoft.XMLHTTP');

for (var i = 0; i < 2; i++) {
httpRequest.open('GET', location.href, true);
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
alert(httpRequest.responseText);
};
};
httpRequest.send(null);
}

the alert shows up twice.

I suspect you set the onreadystatechange handler only once but do
several open/send calls then, I think that is a problem with older
versions of MSXML. So it shouldn't be necessary to create a new object
each time you want to make a request but to ensure that after each open
call the onreadystatechange handler is set.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
First of all, thank you very much for your answer.
I tried your little piece of code and, of course, it worked.
But as soon as I put it in a class, the bug occurs. Here's my code :

function CreateXMLHTTPRequestObject() {
this.xhr_object = new ActiveXObject("Microsoft.XMLHTTP");

this.doRequest = function(url) {
//this.xhr_object = new ActiveXObject("Microsoft.XMLHTTP");

var obj = this;
this.xhr_object.onreadystatechange = function() {
alert("onreadystatechange\nreadyState :
"+obj.xhr_object.readyState);
if(obj.xhr_object.readyState == 4)
alert(obj.xhr_object.responseText);
}

this.xhr_object.open('GET', url, true);
this.xhr_object.send(null);
}
}

var req = new CreateXMLHTTPRequestObject();
req.doRequest(location.href);
req.doRequest(location.href);

As it is, only the first request succeeds. In the second one, the first
alert in the onreadystatechange function never shows up...

But if I uncomment the first line of the function doRequest(url), then
the two requests succeed and all the alerts show up.
What's wrong with my class ?

Jul 23 '05 #3


Robloche wrote:

But as soon as I put it in a class, the bug occurs. Here's my code :

function CreateXMLHTTPRequestObject() {
this.xhr_object = new ActiveXObject("Microsoft.XMLHTTP");

this.doRequest = function(url) {
//this.xhr_object = new ActiveXObject("Microsoft.XMLHTTP");

var obj = this;
this.xhr_object.onreadystatechange = function() {
alert("onreadystatechange\nreadyState :
"+obj.xhr_object.readyState);
if(obj.xhr_object.readyState == 4)
alert(obj.xhr_object.responseText);
}

this.xhr_object.open('GET', url, true);
this.xhr_object.send(null);


I would change the order as follows (first open(), then setting
onreadystatechange, then send()):

this.xhr_object.open('GET', url, true);

var obj = this;
this.xhr_object.onreadystatechange = function() {
alert("onreadystatechange\nreadyState : "+obj.xhr_object.readyState);
if(obj.xhr_object.readyState == 4)
alert(obj.xhr_object.responseText);
}

this.xhr_object.send(null);
Then it works here for me with IE 6 for both requests.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4
Thank you !!!
It was so simple... I feel kind of dumb... :o/

But why does it work only once when the onreadystatechange is set
before opening ?
I went through many web pages that deal with this and I found either
this order or the other one.
Anyway, you saved me hours of debugging, so thanks again !

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christopher Benson-Manica | last post by:
Is there a more crossbrowser-friendly means to make HTTP requests using script than the XML methods? I need to do this in IE 5.0, and it doesn't seem to have the ActiveX control that can make XML...
1
by: Mario Figueiredo | last post by:
Hello everyone, I'm having trouble controlling the cursor position when I make two consecutive calls to the get family of functions. This problem does not happen if there is an output in...
2
by: dmagliola | last post by:
Hello all, I'm experiencing a problem with ASP.Net for which I can't find a reasonable explanation, or any information. I'm currently developing an application that, through AJAX, asks the...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
3
rizwan6feb
by: rizwan6feb | last post by:
Hi experts! Recently i was working on "Form Validation Using Ajax". My form validation was creating problem, when a user changes focus too quickly. I had a post related to this, but was unable to...
7
by: Brent | last post by:
I'm trying to figure out how to iterate over an array that would send a series of XMLHttp requests. I'd like to have each request finish before the next one begins, but I believe that this is not...
1
by: bizt | last post by:
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...
2
by: bizt | last post by:
Hi, I have a page that makes many XmlHttpRequest requests from a single page. This works fine but I need some requests to be made over a secure connection. To my understanding, when setting the...
2
by: MedIt | last post by:
Hi All, I am trying to Post a number of requests to a web service on a client server from a console application.Through this application I am firing some 400 requests to the Web Service, one after...
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...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.