我试图加载一个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://以防出错。

其他回答

在Android应用程序中-例如,允许JavaScript通过file:///android_asset/访问资产-使用setAllowFileAccessFromFileURLs(true)在WebView上调用getSettings()获得的WebSettings上。

如果你使用旧版本的Mozilla Firefox(2019年之前),它会像预期的那样工作,没有任何问题;

附言:令人惊讶的是,旧版本的ie和Edge也能正常工作。

当我在浏览器上加载一个HTML文件时,我得到了这个确切的错误,该浏览器正在使用一个json文件从本地目录。在我的例子中,我能够通过创建一个允许服务器静态内容的简单节点服务器来解决这个问题。我把这个的代码留在了另一个答案。

今天碰到了这个。

我写了一些像这样的代码:

app.controller('ctrlr', function($scope, $http){
    $http.get('localhost:3000').success(function(data) {
        $scope.stuff = data;
    });
});

...但它应该是这样的:

app.controller('ctrlr', function($scope, $http){
    $http.get('http://localhost:3000').success(function(data) {
        $scope.stuff = data;
    });
});

唯一的区别是第二段代码中没有http://。

只是想把它放出来,以防其他人有类似的问题。

它只是说应用程序应该在web服务器上运行。我在chrome上也有同样的问题,我启动tomcat并将我的应用程序移动到那里,它工作了。