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
https://forums.phpfreaks.com/topic/58087-new-at-ajax-ie/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.