2

I'm trying to load a texture but i keep getting this error no matter what i do:

Access to Image at '<file-path>/IMG/1.jpg' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Here's the code i'm using to load the image:

var loader = new THREE.TextureLoader();
loader.crossOrigin = "";
var url = "IMG/1.jpg";
var materials = [
    new THREE.MeshLambertMaterial({
        map: loader.load(url)
    }),
    new THREE.MeshLambertMaterial({
        map: loader.load(url)
    }),
    new THREE.MeshLambertMaterial({
        map: loader.load(url)
    }),
    new THREE.MeshLambertMaterial({
        map: loader.load(url)
    }),
    new THREE.MeshLambertMaterial({
        map: loader.load(url)
    }),
    new THREE.MeshLambertMaterial({
        map: loader.load(url)
    }),
];
WestLangley
  • 102,557
  • 10
  • 276
  • 276
chrisstar123
  • 77
  • 1
  • 2
  • 8

3 Answers3

3

If the file is local you need to run a local server. It's simple in will take just a minute or so.

If the file is not local the server itself needs to give permission. Some servers like github pages do this by default. So does imgur and flickr.

Otherwise if it's a server you control you need to configure it to give permission. Every server (apache, nginx, caddy, iis, etc) are all configured differently and you'll need to search for how based on which server you're using. If it's not a server you control then you need to ask the people that do control it to configure it to give CORS permissions

Cross origin images are not just a matter of setting crossOrigin. All that does is tell the browser you want to ask the server for permission to use the image. It's still up to the server to actually grant that permission. Most servers by default do NOT grant permission and have to be configured to add that permission.

gman
  • 100,619
  • 31
  • 269
  • 393
1

Try this: loader.setCrossOrigin('anonymous');

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
Simon Pasku
  • 539
  • 2
  • 4
  • 17
  • 1
    I have tried this, it doesn't work, the error in the console is the same – chrisstar123 Feb 13 '18 at 11:22
  • then your server seem`s block any request from different domain, you should allow cross origin on your server like here for php https://enable-cors.org/server_php.html – Simon Pasku Feb 13 '18 at 11:38
1

I fixed it by using a different browser than google chrome.

The reason why i couldn't use a local server was because this was a school assignment. The people that were going to grade it weren't going to run a local server to make it work. They'd just open it, see it doesn't work and subtract points.

chrisstar123
  • 77
  • 1
  • 2
  • 8