Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

xml

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

like image 294
eabait Avatar asked Mar 22 '11 19:03

eabait


People also ask

How do I fix Access-Control allow Origin error?

Run the following command to confirm the origin server returns the Access-Control-Allow-Origin header. Replace example.com with the required origin header. Replace https://www.example.net/video/call/System.generateId.dwr with the URL of the resource that's returning the header error.

What does Access-Control allow origin null mean?

Access-Control-Allow-Origin: null This value should not be used to serialize the origin of resources that use a non-hierarchical scheme. Sandboxed documents are defined as null. User agents may grant access to these documents and create a hostile document with null origin.

Why is Origin header null in request?

The Origin spec indicates that the Origin header may be set to "null". This is typically done when the request is coming from a file on a user's computer rather than from a hosted web page. The spec also states that the Origin may be null if the request comes from a "privacy-sensitive" context.


2 Answers

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.

like image 92
Michael McTiernan Avatar answered Sep 19 '22 19:09

Michael McTiernan


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.

like image 27
Juggernaut Avatar answered Sep 19 '22 19:09

Juggernaut