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");
}
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
please take a look at jQuerys ajax section over here: http://api.jquery.com/jQuery.ajax/
personaly i use it aswell
if you want it simple and not relying on a library check this out:
http://www.w3schools.com/Ajax/ajax_example.asp
http://www.w3schools.com/Ajax/tryit.asp?filename=tryajax_first
http://www.w3schools.com/Ajax/ajax_example.asp
http://www.w3schools.com/Ajax/tryit.asp?filename=tryajax_first
how is your code looking like if it doesnt work? it appears that your code is copyed from w3schools.
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
http://social.msdn.microsoft.com/Forums/pl-PL/iewebdevelopment/thread/8c01075f-a547-476a-b6b3-cf095917537a
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
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?
Try this :
http://msdn.microsoft.com/en-us/library/ee797612(v=cs.20).aspx
More infos here :
http://technet.microsoft.com/en-us/library/dd346862.aspx
http://msdn.microsoft.com/en-us/library/ee797612(v=cs.20).aspx
More infos here :
http://technet.microsoft.com/en-us/library/dd346862.aspx
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.
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.
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"?>
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 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.
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you everyone! I was able to tweak it and it still worked on IE and FF locally!
Open in new window