0

I have a question about reading the XML in IE using javascript. I have the following javascript, it should read an XML file. However, I can only read the XML file in FireFox but not IE. Can anyone help me?

It shows 'Access is denied'

function loadXMLDoc(location) {  // location = './abc/abc.xml'
    if (window.XMLHttpRequest)   {
        xhttp=new XMLHttpRequest();
    }
    else  {
        xhttp= new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET",location,false);  // IE mention the error in here
    xhttp.send();
    return xhttp.responseXML;
}

p.s I found the example in here 'http://www.w3schools.com/dom/tryit.asp?filename=try_dom_loadxml'

Francis Gilbert
  • 3,382
  • 2
  • 22
  • 27
John
  • 1,775
  • 4
  • 14
  • 12

1 Answers1

0

My guess is that you are not using a fully qualified URL. Try this:

var location = 'http://yoursite.com/abc/abc.xml';

The way you are writing it, IE is probably thinking this is a local file, not on the server.

Edit: If you want to load a local file, then try this:

var location = 'file:///path/to/abc/abc.xml'; // linux

or

var location = 'C:/path/to/abc/abc.xml'; // windows (I think?)

If you are still getting problems with IE, it's probably to do with IE's security settings. Turn these off or to "low" or whatever and try again.

Edit 2: According to this answer, Chrome browser will not allow ajax requests to file:/// resources deeming local requests to conflict with the same-origin policy. I guess IE implements the same constraint. There is no workaround, so: you'll need to run a server on your localhost. (However yes Firefox does allow these requests.)

Community
  • 1
  • 1
Richard H
  • 38,037
  • 37
  • 111
  • 138
  • I am testing the code in my local machins, could you please to teach me how to do that? my file in the desktop. – John Jul 20 '11 at 10:34
  • thank you for your answer, but I found that it is still not work, I have updated the question which post a link to w3c, would you mind to take a look? – John Jul 20 '11 at 10:59
  • @John: what is the URL of the page in your browser? Is it "http://localhost/yourpage.html' or is it a file path like "C:/path/to/yourpage.html'? – Richard H Jul 20 '11 at 11:04
  • thank you for your reply. The path is 'C:/John/Desktop/abc.xml' – John Jul 20 '11 at 11:09
  • @John: no I mean the path of the actual html page in your browser address bar – Richard H Jul 20 '11 at 11:11
  • it is 'file:///C:/John/Desktop/File_A/File_B/test.html' – John Jul 20 '11 at 11:13
  • ok for the location try: var location = 'file:///C:/John/Desktop/abc.xml'; – Richard H Jul 20 '11 at 11:16
  • thank you for your reply. But it is the same case, it always shows the 'Access is denied.' and show the js file and line number, Code 0 and Char:2, any hits in the Code:0 and Char 2? – John Jul 20 '11 at 11:23