Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 575
Denis Stephanov Avatar asked Jan 31 '17 18:01

Denis Stephanov


People also ask

How do I fix null has blocked by CORS policy?

From Origin 'null' Has Been Blocked By CORS Policy Error. To fix this error is also easy, what you need to do is to create a local web server, and then upload the Html and JS file to the webserver.

How do I fix cross origin requests are only supported for protocol schemes Chrome Chrome extension https?

Just change the url to http://localhost instead of localhost . If you open the html file from local, you should create a local server to serve that html file, the simplest way is using Web Server for Chrome . That will fix the issue.

What is origin null?

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 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

like image 122
Jeremy Iglehart Avatar answered Sep 28 '22 11:09

Jeremy Iglehart


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     }) }); 
like image 42
Sadjad Esfandiari Avatar answered Sep 28 '22 12:09

Sadjad Esfandiari