AJAX problem in Internet Exporer

Hi all.

I have a button on a page that uses AJAX to send a request off to my application server. The application server executes some functionality and then returns a String back to the client. The client then displays this String in an alert box. When I use Mozilla Firefox 1.5 the functionality works fine. It also works no matter how many times I click the button.

However, when I use Internet Explorer 6.0, I run into problems. The first time I click the button, the application server is called and the response is displayed on screen. The second and any subsequent times I click the button the response is displayed on the screen but the application server has not been called. The only way I can get the button to contact the applciation server is to close the window and open a new one. I am lost for ideas on how to overcome this problem.

Javascript is as follows:

<script type="text/javascript">

var xmlhttp

function generateTif(url)

{

xmlhttp=null

// code for Mozilla, etc.

if (window.XMLHttpRequest)

{

xmlhttp=new XMLHttpRequest()

}

// code for IE

elseif (window.ActiveXObject)

{

xmlHttp =new ActiveXObject('Microsoft.XMLHTTP')

}

if (xmlhttp!=null)

{

xmlhttp.onreadystatechange=state_Change

xmlhttp.open("GET",url,true)

xmlhttp.send(null)

}

else

{

alert("Your browser does not support XMLHTTP.")

}

}

function state_Change()

{

// if xmlhttp shows "loaded"

if (xmlhttp.readyState==4)

{

// if "OK"

if (xmlhttp.status==200)

{

// OK response

alert(xmlhttp.responseText);

}

else

{

alert("Problem retrieving XML data")

}

}

}

</script>

The javascript is launched from the following line in the page:

<input type="button" name="genTif" value="Generate TIF" onclick="generateTif('GenerateTif.do');">

Does anyone have any ideas on the problem?

Cheers, Kenny

[3426 byte] By [krodmana] at [2007-11-26 16:13:25]
# 1
Javascript doesn't have nothing to do with Java...MeTitus
Me_Titusa at 2007-7-8 22:36:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Perhaps that this can help:xmlhttp.setRequestHeader("Cache-Control", "no-cache");Do that between the calls to open() and send()
gimbal2a at 2007-7-8 22:36:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Javascript is case sensitive: xmlHttp = new ActiveXObject('Microsoft.XMLHTTP')should be xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
kimseya at 2007-7-8 22:36:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I have updated the script as follows:

<script type="text/javascript">

var xmlhttp

function loadXMLDoc(url)

{

xmlhttp=null

// code for Mozilla, etc.

if (window.XMLHttpRequest)

{

xmlhttp=new XMLHttpRequest()

}

// code for IE

else if (window.ActiveXObject)

{

xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')

}

if (xmlhttp!=null)

{

xmlhttp.onreadystatechange=state_Change

xmlhttp.open("GET",url,true)

xmlhttp.setRequestHeader("Cache-Control", "no-cache")

xmlhttp.send(null)

}

else

{

alert("Your browser does not support XMLHTTP.")

}

}

function state_Change()

{

// if xmlhttp shows "loaded"

if (xmlhttp.readyState==4)

{

// if "OK"

if (xmlhttp.status==200)

{

// OK response

alert(xmlhttp.responseText);

}

else

{

alert("Problem retrieving XML data")

}

}

}

</script>

Thanks for your constructive input but sadly I still have the same problem. Any other thoughts?

Cheers, Kenny

krodmana at 2007-7-8 22:36:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Here is how I do it in my code. The Msxml2.XMLHTTP is for IE 6 and higher I believe, maybe it will work for you. Try getting the ActiveXObject like this:

else if (window.ActiveXObject)

{

try

{

xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

}

kimseya at 2007-7-8 22:36:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

I have found the solution. There is a problem in Internet Explorer with caching. Check out section 4.1 on the following page:

http://en.wikipedia.org/wiki/XMLHttpRequest

I have gone for the solution of attaching a random number to the URL as a parameter. This forces Internet Explorer to go to the application server for each and every request.

Thanks for all your help. The suggestion to set 'no-cache' in header helped my find the solution.

Cheers, Kenny

krodmana at 2007-7-8 22:36:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...