Link to home
Start Free TrialLog in
Avatar of jj1103
jj1103

asked on

How do you get XMLHttpRequest to work in IE9?

This code works on FireFox but not on IE9. I've been all over the online forums. All codes I have tried leaves the page blank in IE9 but renders great in Firefox.  Is there an XMLHttpRequest line specifically for IE9?
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

The problem should be elsewhere, try this :
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
alert("// code for IE7+, Firefox, Chrome, Opera, Safari")
  xmlhttp=new XMLHttpRequest();
alert("AFTER : // code for IE7+, Firefox, Chrome, Opera, Safari")
  }
else
  {// code for IE6, IE5
alert("// code for IE6, IE5")
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
alert("AFTER : // code for IE6, IE5")
alert(typeof xmlhttp); // should not be undefined
  }

Open in new window

did you ever thought about using a library like jQuery?

please take a look at jQuerys ajax section over here: http://api.jquery.com/jQuery.ajax/

personaly i use it aswell
how is your code looking like if it doesnt work? it appears that your code is copyed from w3schools.
Avatar of ParthDalal
ParthDalal

Just a thought, but perhaps it's not a problem with the ActiveXObject, but with the way IE9 handles JavaScript code.  Try adding 'var' in front of your xmlhttp variables and see if that makes a difference.
Found something interesting.  Scroll halfway done the page to see the working AJAX code for IE9:
http://social.msdn.microsoft.com/Forums/pl-PL/iewebdevelopment/thread/8c01075f-a547-476a-b6b3-cf095917537a
Avatar of jj1103

ASKER

I tested it again on IE 9 and ran the debugger. It gave me an "Access Denied" on the line where I open the xml file. So, after researching that some more. I decided to test it on my web server in case it was a local setting, although I have IE set to run local files scripts. Anyway, it worked fine in IE9 once I placed it on the web server.  This doesn't help me much because I was not planning on posting it on a web server.  I don't know what else to do with IE9 settings to allow it to run scripts locally.  Pretty frustrating!
so you want a cross browser solution not working on a webserver?
access denied? why didnt you said that earlyer?

do you try accessing a document on another domain you are running the script in?
if yes, thats why.

you -cannot- access data on other domains through ajax due to some security standard nearly every browser has implemented and enabled by default.

you can just google for "ie ajax access denied" and you will see lot of people with the same problem

example: http://stackoverflow.com/questions/3470859/why-is-ie7-and-ie8-giving-me-access-denied-when-calling-jquery
also.. are you sure the document you try to access has the correct mime type?
also take a look at this
http://developer.yahoo.com/javascript/howto-proxy.html

its an article that explains it pretty well and how you could create a workaround for this problem working for every client connecting to your site.
Avatar of jj1103

ASKER

The xml file and the HTML file are located on my desktop in the same folder. This is the code snippet from the HTML file. It works great in FF but only works in IE9 if these two files are opened via a web server.  Locally on my desktop the page comes up blank. When I right-click to view source all the XML code is displayed.  IE9 is set to execute local scripts too.


<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","data.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

...

The first line of the XML page is:

<?xml version="1.0" encoding="utf-8"?>

Open in new window

you cannot use ajax localy in IE on the fly.

there somewhere is a setting restricting access to local resources set by the security level in IE.
i may try figuring out wich setting it is but for now i will eat something.
there is a solution:

http://stackoverflow.com/questions/436670/local-html-file-ajax-call-and-jquery-woes

just take a look at the heighest voted answer.
The IE browser should be banned, in my opinion.  It results in needless depression for thousands of web developers.  All IE users see is a nice, neat, happy webpage.  Little do they know what's behind it.  They should be informed!
@ParthDalal: i am for TEXT based web pages only for IE <3
@iGottZ: Haha much more reasonable and practical.  An initiative should be started soon.
work for me on IE :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_27268927.html</title>
</head>
<body>
<script type="text/javascript">
	if(window.XMLHttpRequest && navigator.appName.indexOf("Internet Explorer")<0) {
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET","ShoeInventory.xml",false);
		xmlhttp.send();
		xmlDoc = xmlhttp.responseXML;
	}
	else {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.load("ShoeInventory.xml");
	}
	var a = [];
	var path = "//inventory/shoes/Type";
	if(document.evaluate) {
		iterator = xmlDoc.evaluate(path, xmlDoc, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
		var currNode = iterator.iterateNext();
		while(currNode) {
			if(typeof a[currNode.textContent] == "undefined") {
				a[currNode.textContent] = 1;
			}
			else {
				a[currNode.textContent]++;
			}
			currNode = iterator.iterateNext();
		}
	}
	else {
		try {
		var nodes = xmlDoc.selectNodes(path);
		for(i=0;i<nodes.length;i++)  {
			if(typeof a[nodes[i].childNodes[0].nodeValue] == "undefined") {
				a[nodes[i].childNodes[0].nodeValue] = 1;
			}
			else {
				a[nodes[i].childNodes[0].nodeValue]++;
			}
		}
		}
		catch(e) { alert(e) }
	}
	
	for(var i in a) {	
		document.write(i + " Totals:" + a[i] + " <br / >");
	}
	
</script>
</body>
</html>

Open in new window

SOLUTION
Avatar of iGottZ
iGottZ
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jj1103

ASKER

Thank you everyone! I was able to tweak it and it still worked on IE and FF locally!