Access to Image from origin 'null' has been blocked by CORS policy

JavascriptCorsLocalOpenlayers 3

Javascript Problem Overview


I have JavaScript application in OpenLayers 3, and my base layer is created from local tiles. I work only in my computer so I do not know why I have CORS error.

    var newLayer = new ol.layer.Tile({
    source: new ol.source.OSM({
        url: 'E:/Maperitive/Tiles/vychod/{z}/{x}/{y}.png'
    })
});
var schladming = [21.6187, 48.7327]; // longitude first, then latitude
// since we are using OSM, we have to transform the coordinates...
var schladmingWebMercator = ol.proj.fromLonLat(schladming);

var map = new ol.Map({
    layers: [
        newLayer
    ],
    controls: [],
    target: 'mapid',
    view: new ol.View({
        center: schladmingWebMercator,
        zoom: 10,
        minZoom: 10,
        maxZoom: 14
    })
});

error message from console:

> Access to Image at file:///E:/Maperitive/Tiles/vychod/10/573/352.png from origin null has been blocked by CORS policy: Invalid response. Origin null is therefore not allowed access.

When I double-click on image URL, image is opened. Any ideas what is wrong? I never had that error before.

Javascript Solutions


Solution 1 - Javascript

You're running into a CORS error.

Trying to access your file using the local file system doesn't work in your case.

Origin is null because it's your local file system. Could you possibly host this png file?

Suggestion:

Host these files to an AWS S3 bucket instead. Then you can use the http protocol rather than the file protocol. OR setup some http server on your local system and use http to your localhost to serve the files from if you want to keep everything local.

More Reading:

How CORS Works

Solution 2 - Javascript

The problem was actually solved by providing crossOrigin: null to OpenLayers OSM source:

var newLayer = new ol.layer.Tile({
source: new ol.source.OSM({
    url: 'E:/Maperitive/Tiles/vychod/{z}/{x}/{y}.png',
    crossOrigin: null
    })
});

Solution 3 - Javascript

Under the covers there will be some form of URL loading request. You can't load images or any other content via this method from a local file system.

Your image needs to be loaded via a web server, so accessed via a proper http URL.

Solution 4 - Javascript

To solve your error I propose this solution: to work on Visual studio code editor and install live server extension in the editor, which allows you to connect to your local server, for me I put the picture in my workspace 127.0.0.1:5500/workspace/data/pict.png and it works!

Solution 5 - Javascript

For local development you could serve the files with a simple web server.

With Python 2.7 installed, go into the folder where your project is served, like cd my-project/. And then use python -m SimpleHTTPServer which would make index.html and it's JavaScript files available at localhost:8000.

EDIT: For Python 3 use python -m http.server

Solution 6 - Javascript

Try to bypass CORS:

For Chrome: edit shortcut or with cmd: C:\Chrome.exe --disable-web-security

For Firefox: Open Firefox and type about:config into the URL bar. search for: security.fileuri.strict_origin_policy set to false

Solution 7 - Javascript

A solution to this is to serve your code, and make it run on a server, you could use web server for chrome to easily serve your pages.

Solution 8 - Javascript

In this case the CORS problem has been caused by using the wrong source constructor in OpenLayers. ol.source.OSM is intended for accessing the default OpenStreetMap tiles from the web and for that reason defaults to crossOrigin:'anonymous'. If you are using a local source URL you should use the generic ol.source.XYZ constructor which doesn't default the crossOrigin setting (which is why setting crossOrigin:null above happened to work). And it is perfectly legitimate want to use file protocol for maps, for example on an SD card of a mobile device.

Solution 9 - Javascript

I was having the exact same problem. In my case none of the above solutions worked, what did it for me was to add the following:

app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()

So basically, allow everything.

Bear in mind that this is safe only if running locally.

Solution 10 - Javascript

The browser is at the local file system where you're requesting the file. The request was made through XHR. So the origin is mentioned as null. if 'null' is added in the list of protocol schemes supported by CORS, you would access it. But sadly you cant.

Solution 11 - Javascript

just install live server if using vs code in vs code and enable it , it solved the issue in my case.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDenis StephanovView Question on Stackoverflow
Solution 1 - JavascriptJeremy IglehartView Answer on Stackoverflow
Solution 2 - JavascriptSadjad EsfandiariView Answer on Stackoverflow
Solution 3 - JavascriptKitView Answer on Stackoverflow
Solution 4 - Javascriptabdelhadi lebbarView Answer on Stackoverflow
Solution 5 - JavascriptMichael JohansenView Answer on Stackoverflow
Solution 6 - JavascriptS. R.View Answer on Stackoverflow
Solution 7 - JavascriptDamien DoumerView Answer on Stackoverflow
Solution 8 - JavascriptMikeView Answer on Stackoverflow
Solution 9 - JavascriptHugo Nava KoppView Answer on Stackoverflow
Solution 10 - JavascriptsusheelbhargavkView Answer on Stackoverflow
Solution 11 - JavascriptUmasai Anvesh reddy DandaView Answer on Stackoverflow