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

前端 未结 8 1517
野的像风
野的像风 2020-12-01 04:09

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.



        
相关标签:
8条回答
  • 2020-12-01 04:36

    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

    0 讨论(0)
  • 2020-12-01 04:39

    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.

    0 讨论(0)
  • 2020-12-01 04:40

    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.

    0 讨论(0)
  • 2020-12-01 04:41

    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
        })
    });
    
    0 讨论(0)
  • 2020-12-01 04:47

    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

    0 讨论(0)
  • 2020-12-01 04:54

    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!

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