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

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


当前回答

很多问题,我的问题是缺少'/'的例子: jquery-1.10.2.js:8720 XMLHttpRequest无法加载http://localhost:xxxProduct/getList_tagLabels/ 必须是:http://localhost:xxx/Product/getList_tagLabels/

我希望这对遇到这个问题的人有所帮助。

其他回答

我建议您使用一个迷你服务器在本地主机上运行这类应用程序(如果您没有使用某些内置服务器)。

这里有一个非常简单的设置和运行:

https://www.npmjs.com/package/tiny-server

我的水晶球显示,您正在使用文件://或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),它们就被视为不同的起源。

为java安装本地web服务器,例如Tomcat,对于php,您可以使用lamp等 将json文件放到公共可访问的应用服务器目录中 启动应用程序服务器,您应该能够从localhost访问该文件

我将列出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://以防出错。

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