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

后端 未结 5 1721
我在风中等你
我在风中等你 2020-11-30 10:27

This is my code:

this.loadMap = function () {
    this._map = null;
    this._width = 0;
    this._height = 0;
    this._playerX = 0;
    this._playerY = 0;
         


        
相关标签:
5条回答
  • 2020-11-30 10:38

    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.

    0 讨论(0)
  • 2020-11-30 10:47

    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.

    0 讨论(0)
  • 2020-11-30 10:47

    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
    
    0 讨论(0)
  • 2020-11-30 10:53

    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/

    0 讨论(0)
  • 2020-11-30 11:05

    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.

    0 讨论(0)
提交回复
热议问题