0

I'm trying to fetch informations from an XML file on my local machine. It was a success with firefox, but for some reason it doesn't work with IE8 and it has to run on IE7+.

IE doesn't report an error until i add this line to my .js file :

xmlhttp.open("GET","food.xml",false);

I built up my script using W3schools as references and i am using mostly everything like they do here : http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_database

This entire block works on firefox but not on IE, if that can be of any help

xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","food.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

I am sorry and i know this must have been answered already but i don't have the proper vocabulary to find this specific issue.

Tordah
  • 155
  • 2
  • 3
  • 8
  • IE doesn't support XMLHttpRequest. For this particular browser you have to use ActiveX. You might want to check the following answer: http://stackoverflow.com/a/2557268/431287 – rhino Jun 05 '14 at 17:21
  • possible duplicate of [Easiest way to retrieve cross-browser XmlHttpRequest](http://stackoverflow.com/questions/2557247/easiest-way-to-retrieve-cross-browser-xmlhttprequest) – lincolnk Jun 05 '14 at 17:24
  • but it works in the w3schools example! How comes, do you know? Thanks for the link btw, i'm currently reading. – Tordah Jun 05 '14 at 17:24
  • Whoops! I just checked the MSDN page and it seems IE7+ supports XMLHttpRequest, so your code should work in IE8. So I don't know what the problem is. What's the error you get? – rhino Jun 05 '14 at 17:31
  • Could it be because i am working with local files? Because it works on the W3schools example and their files are local as well but it's a web server so i am not sure. Please help :( – Tordah Jun 05 '14 at 18:05

1 Answers1

0

Native XMLHttpRequest support didn't come to IE until IE8. In order to use XHR in IE7, you will have to use the ActiveX object

Here is a good helper function:

function getXMLHttpRequest() 
{
    if (window.XMLHttpRequest) {
        return new window.XMLHttpRequest;
    }
    else {
        try {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch(ex) {
            return null;
        }
    }

The reason you are getting the error is because Internet Explorer's native XMLHTTPRequest object permits requests to HTTP and HTTPS only; requests to FILE, FTP, or other URI schemes are blocked.

To read additional information about XMLHttpRequests in IE, check out the MSDN page: http://msdn.microsoft.com/en-us/library/ie/ms535874(v=vs.85).aspx

  • It doesn't work nor do anything else new. I am using IE8 and i know for sure the xhttp=new XMLHttpRequest(); line works fine but it's the xmlhttp.open("GET","food.xml",false); line that doesn't work. – Tordah Jun 05 '14 at 17:46
  • You have xhttp=new XMLHttpRequest();, but you use **xmlhttp** later. Just a variable name mismatch – Lorenz Meyer Jun 05 '14 at 17:52
  • Thanks. You are right but it was a copy and paste on this website error, i am ussing xmlhttp everywhere. Sorry for the confusion. – Tordah Jun 05 '14 at 17:53