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;
th开发者_运维百科is.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
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.
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.
I had this same issue with Chrome (Version 20.0.1132.57) and using --allow-file-access-from-files didn't work for me (on Ubuntu 12.04).
But, using the command python -m SimpleHTTPServer
in the directory that holds the localfiles I'm trying to test with allows me to work around the problem. That command launches an HTTP server that serves the current directory tree at http://localhost:8000/
So, if I have a file test.html that uses an ajax call for a file in the same dir I can use http://localhost:8000/test.html
and Chrome will be ok with it. For me, this is fine for local dev/testing.
I came across that command on www.commandlinefu.com.
Just to reiterate what Paul & Juggernaut said:
In OSX open terminal and paste this command
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files
You try to access both web page and resources file as file route file:///
You can do following in OSX
open /Applications/Google\ Chrome.app --args --allow-file-access-from-files
try to request web page with local server (http://localhost/your_website.html)
read more about cross site and iFrame at http://weblog.bocoup.com/third-party-javascript-development-future/
精彩评论