"Cross-origin requests are only supported over HTTP." Error loading local file
P粉128563140
2023-08-23 15:30:29
<p>I'm trying to use <code>JSONLoader</code> to load a 3D model stored locally on my computer into Three.js, and the 3D model is in the same directory as the entire website. </p>
<p>I get the<code>"Cross-origin requests are only supported over HTTP."</code> error, but I don't know what causes it or how to fix it. </p>
To be clear - yes, the error states that you cannot point the browser directly to
file://some/path/some.html
Here are some options to quickly start a local web server and let your browser render local files
Python 2
If you have Python installed...
Use the command
cd /path/to/your/folder
Change directory to the filesome.html
or the folder where the file is locatedUse the command
to start the Python web serverpython -m SimpleHTTPServer
This will start a webserver to host your entire directory listing at
http://localhost:8000
python -m SimpleHTTPServer 9000
gives you the link:http://localhost:9000
This method is built into any Python installation.
Python 3
Perform the same steps but use the following command
python3 -m http.server
VSCode
If you use Visual Studio Code, you can install the Live Server extension to provide a local web server environment.
Node.js
Or if you need a more responsive setup and are already using Nodejs...
Install
http-server
by entering
npm install -g http-serverSwitch to your working directory, where
some.html
is locatedStart your http server by issuing
http-server -c-1
http://localhost:8080
rubyMy crystal ball says you are loading the model using
file://
orC:/
which matches the error message as they are not http://So you can either install a web server on your local PC or you can upload the model somewhere else and use jsonp and change the url to http://example.com/path/to/model
Origin is defined in RFC-6454 as
So even if your files originate from the same host (
localhost
), they are considered different as long as the scheme is different (http
/file
) origin of.