我试图加载一个3D模型,存储在我的电脑本地,到Three.js与JSONLoader, 3D模型是在同一目录下,作为整个网站。
我得到了“跨起源请求只支持HTTP.”错误,但我不知道是什么原因导致它也不知道如何修复它。
我试图加载一个3D模型,存储在我的电脑本地,到Three.js与JSONLoader, 3D模型是在同一目录下,作为整个网站。
我得到了“跨起源请求只支持HTTP.”错误,但我不知道是什么原因导致它也不知道如何修复它。
当前回答
对于Linux Python用户:
import webbrowser
browser = webbrowser.get('google-chrome --allow-file-access-from-files %s')
browser.open(url)
其他回答
我将列出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://以防出错。
对于那些在Windows上没有Python或Node.js的人,仍然有一个轻量级的解决方案:Mongoose。
您所要做的就是将可执行文件拖到服务器的根目录,然后运行它。一个图标将出现在任务栏中,它将导航到默认浏览器中的服务器。
此外,Z-WAMP是一个100%可移动的WAMP,在一个文件夹中运行,这是很棒的。如果你需要一个快速的PHP和MySQL服务器,这是一个选择。不过从2013年起就没有更新过。现代的替代方案是Laragon或WinNMP。我还没有对它们进行测试,但它们是可移植的,值得一提。
此外,如果你只想要绝对的基础(HTML+JS),这里有一个小的PowerShell脚本,不需要安装或下载任何东西:
$Srv = New-Object Net.HttpListener;
$Srv.Prefixes.Add("http://localhost:8080/");
$Srv.Start();
Start-Process "http://localhost:8080/index.html";
While($Srv.IsListening) {
$Ctx = $Srv.GetContext();
$Buf = [System.IO.File]::OpenRead((Join-Path $Pwd($Ctx.Request.RawUrl)));
$Ctx.Response.ContentLength64 = $Buf.Length;
$Ctx.Response.Headers.Add("Content-Type", "text/html");
$Buf.CopyTo($Ctx.Response.OutputStream);
$Buf.Close();
$Ctx.Response.Close();
};
这个方法非常简单,它不能显示目录或其他花哨的东西。但是它很好地处理了这些CORS错误。
将脚本保存为服务器。在项目的根目录下运行。它将在它所在的目录中启动index.html。
我的水晶球显示,您正在使用文件://或C:/加载模型,这对错误消息保持真实,因为它们不是http://
因此,您可以在本地PC上安装web服务器,也可以将模型上传到其他地方,使用jsonp并将url更改为http://example.com/path/to/model
RFC-6454中将Origin定义为
...they have the same
scheme, host, and port. (See Section 4 for full details.)
因此,即使您的文件起源于同一主机(localhost),但只要方案不同(http / file),它们就被视为不同的起源。
当我下载一个页面进行离线查看时,我就遇到了这种情况。
我只需要从所有<link>和<script>标签中删除integrity="*****"和crossorigin="anonymous"属性
不可能在没有服务器的情况下加载静态本地文件(例如:svg)。如果你的机器上安装了NPM /YARN,你可以使用"http-server"来设置简单的http服务器。
npm install http-server -g
http-server [path] [options]
或者在项目文件夹中打开终端并键入“hs”。它将自动启动HTTP live服务器。