Ajax problem, wont work in IE

S

squash

The following code works fine in Firefox/Netscape but wont work in IE.
I suspect the problem is with one of these two simple functions. If
there is no obvious error Ill paste the entire code.

///////////////////////////////////////////////////////

function get_last_updated() {

http.open( 'get', url );
http.onreadystatechange = handleHttpResponse;
http.send(null);

setTimeout( 'get_last_updated()', 5000 );

} // end function

///////////////////////////////

function handleHttpResponse() {

if (http.readyState == 4) {

last_updated = http.responseText;

if ( ! first_updated ) { first_updated = last_updated; }

if ( last_updated - first_updated > 0 )
{ window.location = url; }
}
} // end function

///////////////////////////////////////////////////////
 
R

RobG

The following code works fine in Firefox/Netscape but wont work in IE.
I suspect the problem is with one of these two simple functions. If
there is no obvious error Ill paste the entire code.

///////////////////////////////////////////////////////

function get_last_updated() {

http.open( 'get', url );
http.onreadystatechange = handleHttpResponse;
http.send(null);

As a guess, the problem is with wherever you've defined the http object.
It should look something like the stuff here:

<URL:http://jibbering.com/2002/4/httprequest.2004.html>


The relevant part is copied below where it the equivalent object is
called xmlhttp.


Creating the object

In Internet Explorer, you create the object using new
ActiveXObject("Msxml2.XMLHTTP") or new
ActiveXObject("Microsoft.XMLHTTP") depending on the version of MSXML
installed. In Mozilla and Safari (and likely in future UA's that
support it) you use new XMLHttpRequest()

This means that you need to show different script to different
browsers, as what works in one, will error in another. The script
below does this, and if it's not supported, the variable is set to
false to allow for appropriate error messages and recovery with
degrading to more normal HTTP transaction methods when the object
isn't available. This degradation is important, even in IE the objects
can often be blocked by slightly raised security settings (popular due
to the commonly exploited holes of course). Where possible degrade,
some approaches are talked about below, if you really can't, I'd
recommend providing an alternative page aswell. GMail for example has
said they'll be providing a less demanding version in the future,
hopefully with no javascript at all, full degradation.


var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope
// with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
 
S

squash

Thanks, I tried that code but still not working in IE. I think it may
be because I am running an older version of IE6.0 ? I will do a windows
update to see if that fixes it?
 
R

RobG

Thanks, I tried that code but still not working in IE. I think it may
be because I am running an older version of IE6.0 ? I will do a windows
update to see if that fixes it?

You should already have a fully patched Windows system, but I doubt that
it will make any difference. XMLHttpRequest was introduced with IE 5.0.

IE 6.0 is the latest version, the full vision number I have is
6.0.2800.1106.xpsp2.050301-1526. IE 7 beta is around but kinda
irrelevant here.

Maybe it is security related... post a URL.
 
R

Randy Webb

(e-mail address removed) said the following on 2/2/2006 12:50 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
Thanks, I tried that code but still not working in IE.

Post a URL to a sample page that you have made that doesn't work in IE.

I think it may be because I am running an older version of IE6.0 ?

Doubtful but possible.
I will do a windows update to see if that fixes it?

You should update your windows and browser to make sure they are fully
patched but it shouldn't have anything to do with this.
 
S

squash

RobG said:
It doesn't work at all. You added the code to initialise the xmlhttp
object, but kept references to http in your original code. Change one
or the other.

At the top of the script I have a line:

var http = getHTTPObject(); // We create the HTTP Object

so getHTTPObject basically returns the xmlhttp object created inside
it.

I just did the windows update and it is still not working. Could it be
some other security setting inside IE 6, I am using: 6.0.2800.1106
 
R

RobG

At the top of the script I have a line:

var http = getHTTPObject(); // We create the HTTP Object

so getHTTPObject basically returns the xmlhttp object created inside
it.

I just did the windows update and it is still not working. Could it be
some other security setting inside IE 6, I am using: 6.0.2800.1106


I get an error from get_last_updated() just before http.send(). You have:

http.open( 'get', url );
http.onreadystatechange = handleHttpResponse;
http.send(null);


Try:

http.open("GET", url);
http.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
handleHttpResponse();
}
}
xmlhttp.send(null);
 
W

web.dev

At the top of the script I have a line:

var http = getHTTPObject(); // We create the HTTP Object

so getHTTPObject basically returns the xmlhttp object created inside
it.

I would say the main reason why it's not working on Internet Explorer
is that you simply are not creating the xmlhttp object correctly for
IE. In your getHTTPObject function you are doing the following:

try
{
http = new XMLHttpRequest();
}
catch (e)
{
http = false;
}

You are using a method that would obviously would only work on non-IE
browsers. At least until IE7 shows up. Try using the following method
instead to create the XMLHttpRequest object:

if(window.XMLHttpRequest)
{
//for IE7, Mozilla, Safari, etc: use native object
var xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
//use ActiveX control for IE5.x and IE6
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
 
R

Randy Webb

(e-mail address removed) said the following on 2/2/2006 1:20 AM:
Here's a test page I have set up:

http://web.peoriadesignweb.com/dev/ajax/

http://web.peoriadesignweb.com/cgi-bin/court/demo_simple/demo.cgi

If you go to second page and make a reservation, then the first page
should update with a message saying 'Page has been updated':

The problem is not with the XMLHTTPRequest, it is a problem with your
cgi not returning what you think it is or what it should.

Find this section of your code:

if ( ! first_updated ) { first_updated = last_updated; }
if ( last_updated - first_updated > 0 )

Change it to look like this:
if ( ! first_updated ) { first_updated = last_updated; }
document.getElementById('myDiv').innerHTML +=
'First Updated = ' + first_updated +
' and last_updated = ' + last_updated + '<br>';
if ( last_updated - first_updated > 0 )

And then in your body add this HTML:

<div id="myDiv"></div>

Then test it. Then, go make a reservation, and watch your test page.
It doesn't return a modified value. I even added a new Date().getTime()
parameter to the URL to stop it from trying to get it from the cache and
it still doesn't update properly.

Have a look at your cgi script to make sure it is doing what it is
supposed to be doing.
 
R

Randy Webb

web.dev said the following on 2/2/2006 2:10 AM:
I would say the main reason why it's not working on Internet Explorer
is that you simply are not creating the xmlhttp object correctly for
IE.

It is properly being created. The problem is the return value from the
cgi script, not in the HTTPRequest Object.
 
S

squash

I have resolved the issue by changing it from GET to POST. Apparently
it was some caching problem in IE if I use GET.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,295
Latest member
EmilG1510

Latest Threads

Top