4

I think until v5 of Google Chrome the below code worked. Now in the latest version I get the following error when opening my webpage locally:

"XMLHttpRequest cannot load file:///C:/Temp/Course.xml. Cross origin requests are only supported for HTTP."

The Javascript code:

function getXmlDocument(sFile) {
    var xmlHttp, oXML;   
    // try to use the native XML parser
    try {
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", sFile, false); // Use syncronous communication
        xmlHttp.send(null);
        oXML = xmlHttp.responseXML;
    } catch(e) {
        // can't use the native parser, use the ActiveX instead
        xmlHttp = getXMLObject();
        xmlHttp.async = false;            // Use syncronous communication
        xmlHttp.resolveExternals = false;
        xmlHttp.load(sFile);
        oXML = xmlHttp;
    }
    // return the XML document object
    return oXML;
}

// get the best ActiveX object that can read XML
function getXMLObject() {
    // create an array with the XML ActiveX versions
    var aVersions = new Array("Msxml2.DOMDocument.6.0", "Msxml2.DOMDocument.3.0");

    // loop through the array until we can create an activeX control
    for (var i=0; i<aVersions.length; i++) {
        // return when we can create the activeX control
        try {
            var oXML = new ActiveXObject(aVersions[i]);
            return oXML;
        } 
        catch(e) {
        }
    }
    // could not create an activeX, return a null
    return null;
}

I really don't want to be forced to open the web page from a web server every time.

2 Answers 2

6

Local file access is disabled by default for security reasons. Try starting Google Chrome from the command line with the argument --allow-file-access

Sign up to request clarification or add additional context in comments.

1 Comment

It appears that, at least as of Chrome 24, you now need a separate flag to access local files via Ajax: --allow-file-access-from-files.
0

It would be more secure if you just start a local webserver and fetch your html and xml from localhost.

You can easily avoid deploying of the files by just let the server serve the contents of a local folder in which you place your xml.

This way you avoid

  • having to start chrome in an unsecure mode
  • having problems when you later deploy your app to a server on the internet

server to go is an example for an easy to install webserver http://www.server2go-web.de/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.