“Origin null is not allowed by Access-Control-Allow-Origin” error for request made by application running from a file:// URL

后端 未结 17 1380
孤城傲影
孤城傲影 2020-11-21 04:42

I\'m developing a page that pulls images from Flickr and Panoramio via jQuery\'s AJAX support.

The Flickr side is working fine, but when I try to $.get(url, ca

相关标签:
17条回答
  • 2020-11-21 05:21

    There is a small problem in the solution posted by CodeGroover above , where if you change a file, you'll have to restart the server to actually use the updated file (at least, in my case).

    So searching a bit, I found this one To use:

    sudo npm -g install simple-http-server # to install
    nserver # to use
    

    And then it will serve at http://localhost:8000.

    0 讨论(0)
  • 2020-11-21 05:23

    I use Apache server, so I've used mod_proxy module. Enable modules:

    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    

    Then add:

    ProxyPass /your-proxy-url/ http://service-url:serviceport/
    

    Finally, pass proxy-url to your script.

    0 讨论(0)
  • 2020-11-21 05:29

    I also got the same error in Chrome (I didn't test other browers). It was due to the fact that I was navigating on domain.com instead of www.domain.com. A bit strange, but I could solve the problem by adding the following lines to .htaccess. It redirects domain.com to www.domain.com and the problem was solved. I am a lazy web visitor so I almost never type the www but apparently in some cases it is required.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
    
    0 讨论(0)
  • 2020-11-21 05:35

    You need to maybe add a HEADER in your called script, here is what I had to do in PHP:

    header('Access-Control-Allow-Origin: *');
    

    More details in Cross domain AJAX ou services WEB (in French).

    0 讨论(0)
  • 2020-11-21 05:36

    For a simple HTML project:

    cd project
    python -m SimpleHTTPServer 8000
    

    Then browse your file.

    0 讨论(0)
  • 2020-11-21 05:36

    For PHP - this Work for me on Chrome, safari and firefox

    https://w3c.github.io/webappsec-cors-for-developers/#avoid-returning-access-control-allow-origin-null

    header('Access-Control-Allow-Origin: null');
    

    using axios call php live services with file://

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