The rewritten title is: "Cross-domain requests only support HTTP" error message encountered when loading local files
P粉680487967
2023-08-21 14:43:40
<p>I'm trying to use <code>JSONLoader</code> to load a 3D model stored on my computer in the same directory as the entire website into Three.js. </p>
<p>I encountered the error <code>"Cross-origin requests only support HTTP protocol"</code>, but I don't know what causes it or how to fix it. </p>
Just to be clear - yes, the error is saying that you cannot point directly in the browser to
file://some/path/some.html
Here are some options for quickly launching a local web server to let your browser render local files
Python 2
If you already have Python installed...
Use the command
cd /path/to/your/folder
Go to the folder containing your filesome.html
or fileUse command
python -m SimpleHTTPServer
Start the Python Web serverThis will start a web server on
http://localhost:8000
to host your entire directory listingpython -m SimpleHTTPServer 9000
, the link is: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 are using Visual Studio Code, you can install the Live Server extension, which provides a local web server environment.
Node.js
Or if you need a more responsive setup and are already using nodejs...
By entering
npm install -g http-server
Installhttp-server
Switch to the working directory containing
some.html
Start your http server by issuing
http-server -c-1
http://localhost:8080
RubyMy crystal ball tells me that you are loading the model using
file://
orC:/
, which fits the error message since they are nothttp:// /
So you can install a web server on your local machine, or upload the model elsewhere, and use
jsonp
and change the URL tohttp://example.com/path/ to/model
Origin is defined in RFC-6454
So even if your files originate from the same host (
localhost
), they are considered different as long as the scheme is different (http
/file
) the origin.