“仅 HTTP 支持跨源请求。”加载本地文件时出错
P粉128563140
P粉128563140 2023-08-23 15:30:29
0
2
416
<p>我正在尝试使用 <code>JSONLoader</code> 将本地存储在计算机上的 3D 模型加载到 Three.js 中,并且该 3D 模型与整个网站位于同一目录中。</p> <p>我收到<code>“跨源请求仅支持 HTTP。”</code> 错误,但我不知道是什么原因导致的,也不知道如何修复它。</p>
P粉128563140
P粉128563140

répondre à tous(2)
P粉868586032

明确地说 - 是的,错误表明您无法将浏览器直接指向 file://some/path/some.html

这里有一些选项可以快速启动本地网络服务器,让您的浏览器呈现本地文件

Python 2

如果你安装了Python...

  1. 使用命令cd /path/to/your/folder将目录更改为文件some.html或文件所在的文件夹

  2. 使用命令 python -m SimpleHTTPServer

    启动 Python Web 服务器

这将启动一个网络服务器来托管您的整个目录列表,网址为http://localhost:8000

  1. 您可以使用自定义端口 python -m SimpleHTTPServer 9000 为您提供链接:http://localhost:9000

此方法内置于任何 Python 安装中。

Python 3

执行相同的步骤,但使用以下命令python3 -m http.server

VSCode

如果您使用Visual Studio Code,您可以安装Live Server 扩展,提供本地 Web 服务器环境。

Node.js

或者,如果您需要更具响应性的设置并且已经使用了 Nodejs...

  1. 通过输入 npm install -g http-server 安装 http-server

  2. 切换到您的工作目录,some.html 所在的位置

  3. 通过发出http-server -c-1启动您的http服务器

这会启动 Node.js httpd,它将目录中的文件作为静态文件提供,可从 http://localhost:8080

红宝石

如果您的首选语言是 Ruby ...Ruby Gods 说这也有效:

ruby -run -e httpd . -p 8080

PHP

当然PHP也有它的解决方案。

php -S localhost:8000
P粉710478990

我的水晶球说您正在使用 file://C:/ 加载模型,这与错误消息相符,因为它们不是http://

因此,您可以在本地 PC 上安装网络服务器,也可以将模型上传到其他位置并使用 jsonp 并将 url 更改为 http://example.com/path/to/型号

起源在 RFC-6454 中定义为

...they have the same
   scheme, host, and port.  (See Section 4 for full details.)

因此,即使您的文件源自同一主机 (localhost),但只要方案不同 (http / file ),它们被视为不同的来源。

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!