Jump to content

New at AJAX, IE


Thierry

Recommended Posts

I'm getting my hand at AJAX and I've got a small problem.

I'm simply using AJAX to test a request to the contents from an XML document and in Firefox it works perfectly, but in IE the contents never update, but they do in Firefox.

 

As I'm new with AJAX its probably a small issue but I can't seem to find it.

 

<html>
<head>
<title>AJAX test</title>
<script language="javascript">
<!--
function data_request(url) {
var httpRequest = "";

if(!(window.XMLHttpRequest == null)){ // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
//if we dont have a Content Type (format), we make it XML
if(!(httpRequest.overrideMimeType == null)){
httpRequest.overrideMimeType('text/xml');}
//attempt 1 for IE
}else if(!(window.ActiveXObject == null)) { // IE
try{
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
httpRequest.setHeader("Cache-Control", "no-cache");}
//attempt 2 for IE
catch(e){
try{httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
httpRequest.setHeader("Cache-Control", "no-cache");} 
catch(e){}}
} //end of if else

//if we can't create the XML request, we end
if(!httpRequest){
alert('Giving up  Cannot create an XMLHTTP instance');
return false;}

//ok, now we are gonna do our thing and then send the request
httpRequest.onreadystatechange = function(){alertContents(httpRequest);}
httpRequest.open('GET', url, true);
httpRequest.send('');

//and our thing
function alertContents(httpRequest) {

//ready state values:
// 0 (uninitialized)
// 1 (loading)
// 2 (loaded)
// 3 (interactive)
// 4 (complete) 
if(httpRequest.readyState == 4){
//status code of the HTTP server response, 200 is ok to go
if (httpRequest.status == 200){
var xmldoc = httpRequest.responseXML;
var root_node = xmldoc.getElementsByTagName('root').item(0);
alert(root_node.firstChild.data);}else{
alert('There was a problem with the request.');}}
} //end of alertContents
} //end of data_request
-->
</script>
</head>
<input type="button" name="test" id="test" value="Test" onclick="data_request('testing.xml')">
<body>
</body>
</html>

 

Any hint of what I'm doing wrong? (it works fine in Firefox 2.0)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.