我试图加载一个3D模型,存储在我的电脑本地,到Three.js与JSONLoader, 3D模型是在同一目录下,作为整个网站。
我得到了“跨起源请求只支持HTTP.”错误,但我不知道是什么原因导致它也不知道如何修复它。
我试图加载一个3D模型,存储在我的电脑本地,到Three.js与JSONLoader, 3D模型是在同一目录下,作为整个网站。
我得到了“跨起源请求只支持HTTP.”错误,但我不知道是什么原因导致它也不知道如何修复它。
当前回答
科尔多瓦实现了这一点。我还是不明白科多瓦是怎么做到的。它甚至不通过shouldInterceptRequest。
后来我发现,从本地加载任何文件的关键是:myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
当你想要访问任何http资源时,webview会用OPTIONS方法检查,你可以通过WebViewClient授予访问权限。shouldInterceptRequest通过返回一个响应,对于下面的GET/POST方法,您可以只返回null。
其他回答
只是显式-是的,错误是说你不能将浏览器直接指向file://some/path/some.html
这里有一些选项可以快速启动本地web服务器,让浏览器呈现本地文件
Python 2
如果你安装了Python…
使用cd /path/to/your/folder命令将目录更改为文件some.html所在的文件夹 使用Python -m SimpleHTTPServer命令启动一个Python web服务器
这将启动一个web服务器,在http://localhost:8000上托管您的整个目录列表
你可以使用自定义端口python -m SimpleHTTPServer 9000给你链接:http://localhost:9000
这种方法内置于任何Python安装中。
Python 3
执行相同的步骤,但使用以下命令代替python3 -m http.server
VSCode
如果你正在使用Visual Studio Code,你可以安装Live Server扩展,它提供了一个本地web服务器环境。
node . js
或者,如果你需要一个更灵敏的设置,并且已经使用了nodejs…
通过输入npm Install -g http-server安装http-server 切换到你的工作目录,yoursome.html所在的目录 通过发出http-server -c-1启动http服务器
这将启动一个Node.js httpd,它将目录中的文件作为可从http://localhost:8080访问的静态文件提供
Ruby
如果你的首选语言是Ruby…红宝石之神说这也管用:
ruby -run -e httpd . -p 8080
PHP
当然,PHP也有它的解决方案。
php -S localhost:8000
今天碰到了这个。
我写了一些像这样的代码:
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://。
只是想把它放出来,以防其他人有类似的问题。
科尔多瓦实现了这一点。我还是不明白科多瓦是怎么做到的。它甚至不通过shouldInterceptRequest。
后来我发现,从本地加载任何文件的关键是:myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
当你想要访问任何http资源时,webview会用OPTIONS方法检查,你可以通过WebViewClient授予访问权限。shouldInterceptRequest通过返回一个响应,对于下面的GET/POST方法,您可以只返回null。
我将列出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://以防出错。
如果您坚持在本地运行.html文件,而不是将其与web服务器一起提供,那么您可以通过使有问题的资源内联可用来防止这些跨起源请求的发生。
当我试图通过file://来提供.js文件时,我遇到了这个问题。我的解决方案是更新我的构建脚本,以取代<script src="…">标签与<script>…</script>. " 这里有一个吞咽的方法:
1. 运行NPM install——save-dev来安装gulp、gulp-inline和del包。
2. 在根目录中创建gulpfile.js后,添加以下代码(只需更改文件路径以适合自己):
let gulp = require('gulp');
let inline = require('gulp-inline');
let del = require('del');
gulp.task('inline', function (done) {
gulp.src('dist/index.html')
.pipe(inline({
base: 'dist/',
disabledTypes: 'css, svg, img'
}))
.pipe(gulp.dest('dist/').on('finish', function(){
done()
}));
});
gulp.task('clean', function (done) {
del(['dist/*.js'])
done()
});
gulp.task('bundle-for-local', gulp.series('inline', 'clean'))
要么运行gulp bundle-for-local,要么更新构建脚本以自动运行它。
您可以在这里看到我的案例的详细问题和解决方案。