我试图加载一个3D模型,存储在我的电脑本地,到Three.js与JSONLoader, 3D模型是在同一目录下,作为整个网站。

我得到了“跨起源请求只支持HTTP.”错误,但我不知道是什么原因导致它也不知道如何修复它。


当前回答

我将列出3种不同的方法来解决这个问题:

Using a very lightweight npm package: Install live-server using npm install -g live-server. Then, go to that directory open the terminal and type live-server and hit enter, page will be served at localhost:8080. BONUS: It also supports hot reloading by default. Using a lightweight Google Chrome app developed by Google: Install the app, then go to the apps tab in Chrome and open the app. In the app point it to the right folder. Your page will be served! Modifying Chrome shortcut in windows: Create a Chrome browser's shortcut. Right-click on the icon and open properties. In properties, edit target to "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir="C:/ChromeDevSession" and save. Then using Chrome open the page using ctrl+o. NOTE: Do NOT use this shortcut for regular browsing.

注意:使用类似http://localhost:8080的http://以防出错。

其他回答

对于Linux Python用户:

import webbrowser
browser = webbrowser.get('google-chrome --allow-file-access-from-files %s')
browser.open(url)

不可能在没有服务器的情况下加载静态本地文件(例如:svg)。如果你的机器上安装了NPM /YARN,你可以使用"http-server"来设置简单的http服务器。

npm install http-server -g
http-server [path] [options]

或者在项目文件夹中打开终端并键入“hs”。它将自动启动HTTP live服务器。

加载本地文件的一种方法是在项目文件夹中使用它们,而不是在项目文件夹之外。在您的项目示例文件下创建一个文件夹,类似于我们创建图像的方式,并替换其中使用完整的本地路径而不是项目路径的部分,并使用项目文件夹下的文件的相对url。 这对我很有效

科尔多瓦实现了这一点。我还是不明白科多瓦是怎么做到的。它甚至不通过shouldInterceptRequest。

后来我发现,从本地加载任何文件的关键是:myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);

当你想要访问任何http资源时,webview会用OPTIONS方法检查,你可以通过WebViewClient授予访问权限。shouldInterceptRequest通过返回一个响应,对于下面的GET/POST方法,您可以只返回null。

只需将url更改为http://localhost而不是localhost。如果你从本地打开html文件,你应该创建一个本地服务器来服务该html文件,最简单的方法是使用Web server for Chrome。这样问题就解决了。