Error: "Origin null is not allowed by Access-Control-Allow-Origin" when loading an XML file with JQuery's ajax method

This is my code:

this.loadMap = function () {
    this._map = null;
    this._width = 0;
    this._height = 0;
    this._playerX = 0;
    this._playerY = 0;
    this.finished = false;
    this.loaded = false;
    $.ajax({
        type: "GET",
        url: "maze1.xml",
        dataType: "xml",
        success: this.parseXmlMap,
        context: this
    });
};

The error i'm getting is

"XMLHttpRequest cannot load file:///C:/wamp/www/mazegame/maze1.xml. Origin null is not allowed by Access-Control-Allow-Origin".

This same script works fine in Firefox


Solution 1:

You're testing this in Chrome? What's basically happening is because you're loading the file from your filesystem instead of from a server, Chrome is setting your origin to null even though the resource you're requesting is local to you. If you were to do this from an HTTP server such as Apache, I think it would work just fine.

Solution 2:

Yes, Google in their blessed wisdom decided that Chrome will not permit an access to local files for rather obscure security reasons. Every two local files are considered as if they were from different domains, and accessing a local file is considered a cross-site request.

There is a workaround, but useful only in some situations: If you can run Chrome with a command line parameter --allow-file-access-from-files, the security check will not be done.