&quot'Cross Origin请求仅支持HTTP。”加载本地文件时出错
我正在尝试将3D模型加载到本地存储在我的计算机上,并使用 jsonloader
将3D模型与整个网站相同的目录中。
我得到“跨越来源的请求仅支持HTTP。”
错误,但我不知道是什么原因导致了它,也不知道如何修复它。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
我的水晶球说您正在使用
file://
或c:/
加载模型,因为它们不是http ://
,因此您可以在本地PC中安装Web服务器,也可以将模型上传到其他地方,然后使用
JSONP
,然后将URL更改为http://example.com/路径/to/model
oneration在 rfc-6454
尽管您的文件源自同一主机(
localhost
),但只要该方案不同(http
/file> file
),它们就会得到处理。作为不同的起源。My crystal ball says that you are loading the model using either
file://
orC:/
, which stays true to the error message as they are nothttp://
So you can either install a webserver in your local PC or upload the model somewhere else and use
jsonp
and change the url tohttp://example.com/path/to/model
Origin is defined in RFC-6454 as
So even though your file originates from the same host (
localhost
), but as long as the scheme is different (http
/file
), they are treated as different origin.只是要明确 - 是的,错误是说您不能直接指向浏览器
file> file://some/path/some.html
这里有一些选择,可以快速旋转本地Web服务器以便 则浏览器渲染本地文件
python
如果您安装了python,
将目录更改为文件夹中的文件
some.html < /code>或使用命令
cd /path存在文件(s) /to/您的/文件夹
启动python Web服务器使用以下命令之一
之一
python -m simplehttpserver#python 2
python3 -m http.server#python 3
这将启动一个Web服务器,以托管您的整个目录列表,at http:// localhost:8000
python3 -m http.server 9000
提供链接: http:// localhost:9000 要指向您的浏览器对此方法的指向任何Python安装。
vscode
如果您使用的是 Visual Studio Code 您可以安装
node.js
,如果您需要更响应的设置,并且已经使用nodejs ...
install
http-server
通过键入NPM安装-G http -server
更改为工作目录,其中
some.html
lives lives < /p>通过发行
http-server -c-c-1
启动http服务器
然后旋转一个node.js httpd,该httpd将目录中的文件服务为静态文件,可从 http:// localhost:8080
ruby
如果您喜欢的语言是Ruby ... Ruby gods也说这也有效:
PHP
当然也具有解决方案。
Just to be explicit - Yes, the error is saying you cannot point your browser directly at
file://some/path/some.html
Here are some options to quickly spin up a local web server to let your browser render local files
Python
If you have Python installed...
Change directory into the folder where your file
some.html
or file(s) exists using the commandcd /path/to/your/folder
Start up a Python web server using one of below commands
python -m SimpleHTTPServer # python 2
python3 -m http.server # python 3
This will start a web server to host your entire directory listing at http://localhost:8000
python3 -m http.server 9000
giving you link: http://localhost:9000 to point your browser atThis approach is built in to any Python installation.
VSCode
If you are using Visual Studio Code you can install the Live Server extension which provides a local web server ... after installing this extension click on
Go Live
widget on bottom of vscode window to launch browser pointing at your code dirNode.js
Alternatively, if you demand a more responsive setup and already use nodejs...
Install
http-server
by typingnpm install -g http-server
Change into your working directory, where your
some.html
livesStart your http server by issuing
http-server -c-1
This spins up a Node.js httpd which serves the files in your directory as static files accessible from http://localhost:8080
Ruby
If your preferred language is Ruby ... the Ruby Gods say this works as well:
PHP
Of course PHP also has its solution.
在Chrome中,您可以使用此标志:
在此处阅读更多:如何使用chrome at” - “ hallow-file-access-from-files”模式启动HTML?
In Chrome you can use this flag:
Read more here: How to launch html using Chrome at "--allow-file-access-from-files" mode?
今天跑去。
我编写了一些看起来像这样的代码:
...但是应该看起来像这样:
唯一的区别是代码第二段中缺少
http://
。只是想在其他情况下遇到类似问题的其他问题。
Ran in to this today.
I wrote some code that looked like this:
...but it should've looked like this:
The only difference was the lack of
http://
in the second snippet of code.Just wanted to put that out there in case there are others with a similar issue.
只需将URL更改为
http:// localhost
而不是localhost
。如果您从本地打开HTML文件,则应创建一个本地服务器来服务该html文件,最简单的方法是使用for Chrome
。这将解决这个问题。Just change the url to
http://localhost
instead oflocalhost
. If you open the html file from local, you should create a local server to serve that html file, the simplest way is usingWeb Server for Chrome
. That will fix the issue.我将列出 3种不同的方法来解决此问题:
npm
package :安装 live-server 使用npm install -g install -g live-server
。然后,转到该目录打开终端和类型live-server
并命中Enter,页面将通过localhost:8080
提供。奖励:默认情况下还支持热重新加载。target
to“ C:\ Program Files(x86)\ Google \ Chrome \ Chrome \ application \ chrome.exe” - disable-web-security -user-data-data-data-data-data =“ C:/Chromedevsession”
并保存。然后使用Chrome使用Ctrl+O
打开页面。注意:做不使用此快捷方式进行常规浏览。注意:使用
http://
喜欢http:// localhost:8080
,以防您面临错误。I'm going to list 3 different approaches to solve this issue:
npm
package: Install live-server usingnpm install -g live-server
. Then, go to that directory open the terminal and typelive-server
and hit enter, page will be served atlocalhost:8080
. BONUS: It also supports hot reloading by default.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 usingctrl+o
. NOTE: Do NOT use this shortcut for regular browsing.Note: Use
http://
likehttp://localhost:8080
in case you face error.使用
http://
或https://
创建URL错误:
localhost:8080
解决方案解决方案:
http:// localhost:8080
Use
http://
orhttps://
to create urlerror:
localhost:8080
solution:
http://localhost:8080
在Android应用程序中 - 例如,允许JavaScript可以通过
file:/// Android_asset/
访问资产 - 使用setallowfileafileAccessFromfileUrls(true)(true)
您从调用getSettings()
在WebView
上获得。In an Android app — for example, to allow JavaScript to have access to assets via
file:///android_asset/
— usesetAllowFileAccessFromFileURLs(true)
on theWebSettings
that you get from callinggetSettings()
on theWebView
.对我来说最快的方法是:
对于Windows用户,在解决Firefox问题上运行文件,或
如果您想对我使用Chrome最简单的方法是安装Python 3,则从命令提示符命令
Python -M http.server
然后转到 http:// localhost:8000/然后导航到您的文件fastest way for me was:
for windows users run your file on Firefox problem solved, or
if you want to use chrome easiest way for me was to install Python 3 then from command prompt run command
python -m http.server
then go to http://localhost:8000/ then navigate to your files简单的解决方案使用VS代码
我已经遇到了一段时间。大多数答案有效。但是我找到了一个不同的解决方案。如果您不想处理
node.js
或此处的任何其他解决方案,并且正在使用HTML文件(从另一个JS文件或Fetch JSON API的调用函数)尝试使用 live服务器扩展。它使您可以轻松打开Live服务器。由于它创建
,然后单击使用Live Server 打开
localhost
服务器,因此问题正在解决。您可以通过打开HTML文件并在编辑器上右键单击Localhost
启动。
它基本上是使用
http://localhost/index.html
而不是使用file> file> file> file> file>:// ...
。编辑
不必拥有
.html
文件。您可以使用快捷方式启动Live服务器。希望它能帮助某人:)
Easy solution for whom using VS Code
I've been getting this error for a while. Most of the answers works. But I found a different solution. If you don't want to deal with
node.js
or any other solution in here and you are working with an HTML file (calling functions from another js file or fetch json api's) try to use Live Server extension.It allows you to open a live server easily. And because of it creates
localhost
server, the problem is resolving. You can simply start thelocalhost
by open a HTML file and right-click on the editor and click onOpen with Live Server
.It basically load the files using
http://localhost/index.html
instead of usingfile://...
.EDIT
It is not necessary to have a
.html
file. You can start the Live Server with shortcuts.Hope it will help someone :)
如果您使用旧版本的Mozilla Firefox(2019年前),它将按预期工作,没有任何问题;
PS令人惊讶的是,旧版本的Internet Explorer&amp;边缘的工作也绝对很好。
If you use old version of Mozilla Firefox (pre-2019), it will work as expected without any issues;
P.S. Surprisingly, old versions of Internet Explorer & Edge work absolutely fine too.
更新:我亲自关闭
security.fileuri.strict_origin_policy_policy
在Firefox中通过关于:config
。它允许提取
可以在我的本地HTML文件中工作,因此我可以在不必启动服务器的情况下测试工作。与Chrome不同,Firefox无需重新启动即可更改此政策。我不知道这可能会如何影响正常的浏览。我认为它仅影响您打开的本地.html文件。但是Firefox无论如何都不是我的主要浏览器,所以我只是将其保留。对于那些没有python或node.js的窗户上的人,仍然有一个轻巧的解决方案: mongoose /strong>。
您所要做的就是将可执行文件拖到服务器的根部所在的任何地方,然后运行它。图标将显示在任务栏中,并将在默认浏览器中导航到服务器。
另外, z-wamp 是一个100%的便携式wamp,在单个文件夹中运行,很棒。如果您需要快速的PHP和MySQL Server,则可以选择。尽管自2013年以来就没有进行更新。现代替代方案是 laragon 或 winnmp 。我没有测试过它们,但是它们是便携式的,值得一提的。我也是
另外,如果您只想要绝对的基础知识(HTML+JS),这是一个微小的PowerShell脚本,不需要安装或下载任何内容:
此方法非常准确,它不能显示目录或其他奇特的东西。但是它处理了这些CORS错误。
将脚本保存为
server.ps1
,并在项目的根部运行。它将在放置的目录中启动index.html。Update: I personally turn off
security.fileuri.strict_origin_policy
in Firefox viaabout:config
. It allowsfetch
to work in my local html files so I can test my work without having to start a server. Unlike Chrome, Firefox doesn't need to be restarted to change this policy. I have no clue how this may impact normal browsing; I assume it only affects local .HTML files you open. But Firefox is not my primary browser anyway, so I just leave it on.For those on Windows without Python or Node.js, there is still a lightweight solution: Mongoose.
All you do is drag the executable to wherever the root of the server should be, and run it. An icon will appear in the taskbar and it'll navigate to the server in the default browser.
Also, Z-WAMP is a 100% portable WAMP that runs in a single folder, it's awesome. That's an option if you need a quick PHP and MySQL server. Though it hasn't been updated since 2013. A modern alternative would be Laragon or WinNMP. I haven't tested them, but they are portable and worth mentioning. I also was a fan of Uniform Server years ago; but it seems a bit dated, yet still around.
Also, if you only want the absolute basics (HTML+JS), here's a tiny PowerShell script that doesn't need anything to be installed or downloaded:
This method is very barebones, it cannot show directories or other fancy stuff. But it handles these CORS errors just fine.
Save the script as
server.ps1
and run in the root of your project. It will launch index.html in the directory it is placed in.我怀疑在某些答案中已经提到了它,但是我会稍微修改以使其具有完整的工作答案(更易于找到和使用)。
转到: https://nodejs.org/en/down/down/load/ 。安装nodejs。
通过命令提示
npm Install -G http-server
更改为您的工作目录,其中
index.html
/youserome.html
居住。通过运行命令
http-server -c-1
启动您的HTTP服务器,
Open Open Web浏览器到
http:// localhost:8080
或
http:// localhost:8080/youserome.html
- 取决于您的HTML文件名。I suspect it's already mentioned in some of the answers, but I'll slightly modify this to have complete working answer (easier to find and use).
Go to: https://nodejs.org/en/download/. Install nodejs.
Install http-server by running command from command prompt
npm install -g http-server
.Change into your working directory, where
index.html
/yoursome.html
resides.Start your http server by running command
http-server -c-1
Open web browser to
http://localhost:8080
or
http://localhost:8080/yoursome.html
- depending on your html filename.当在使用本地目录的JSON文件的浏览器上加载HTML文件时,我会遇到此确切的错误。就我而言,我能够通过创建允许使用服务器静态内容的简单节点服务器来解决此问题。我在此其他答案。
I was getting this exact error when loading an HTML file on the browser that was using a json file from the local directory. In my case, I was able to solve this by creating a simple node server that allowed to server static content. I left the code for this at this other answer.
它只是说该应用程序应在Web服务器上运行。我对Chrome遇到了同样的问题,我开始使用Tomcat并将其应用于那里,并且奏效了。
It simply says that the application should be run on a web server. I had the same problem with chrome, I started tomcat and moved my application there, and it worked.
我建议您使用迷你服务器在Localhost上运行此类应用程序(如果您不使用某些内置服务器)。
这是一个非常简单的设置和运行:
I suggest you use a mini-server to run these kind of applications on localhost (if you are not using some inbuilt server).
Here's one that is very simple to setup and run:
没有服务器,无法加载静态本地文件(例如:SVG)。如果计算机中安装了NPM/YARN,则可以使用“ http-serverrer”> http-serverrer ”
Not possible to load static local files(eg:svg) without server. If you have NPM /YARN installed in your machine, you can setup simple http server using "http-server"
当我下载页面以供离线视图时,经历了这一点。
我只需要删除
Integrity =“ *****”
andcrossorigin =“ Anonymous”
属性,从所有&lt; link&gt;
and&lt; script&gt;
标签Experienced this when I downloaded a page for offline view.
I just had to remove the
integrity="*****"
andcrossorigin="anonymous"
attributes from all<link>
and<script>
tags如果您坚持在本地运行
.html
文件,而不是与Web服务器一起使用,则可以通过使有问题的资源内联用来防止这些交叉原点请求进行。我试图通过
file> file> file> file> file> file> file> file> file> file>。我的解决方案是更新我的构建脚本以替换
&lt; script src =“ ...”&gt;
标签,用&lt; script&gt; ...&lt;/script&gt;
。这是
Gulp
做到这一点的方法:1。
运行
npm install -save-dev
到软件包Gulp
,Gulp-Inline
和del
。2。
创建
gulpfile.js
到根目录之后,添加以下代码(只需更改您适合您的任何适合您的文件路径):Gulp Bundle-for-local
或更新您的构建脚本以自动运行它。您可以看到我的案例在这里的详细问题和解决方案。
If you insist on running the
.html
file locally and not serving it with a webserver, you can prevent those cross origin requests from happening in the first place by making the problematic resources available inline.I had this problem when trying to to serve
.js
files throughfile://
. My solution was to update my build script to replace<script src="...">
tags with<script>...</script>
.Here's a
gulp
approach for doing that:1.
run
npm install --save-dev
to packagesgulp
,gulp-inline
anddel
.2.
After creating a
gulpfile.js
to the root directory, add the following code (just change the file paths for whatever suits you):gulp bundle-for-local
or update your build script to run it automatically.You can see the detailed problem and solution for my case here.
对于
macos
...设置一个简单的启动,在您自己的 chrome ...保存 中启用这些迷人的功能,以启用这些迷人的功能...
plist
,名为 whyther (aunplion.chrome.dev.mode.plist
)) /code>具有与...相似的内容应在启动时启动。但是,您可以随时使用终端命令
laundingctl load -w〜/Library/ launchagents/lunage.chrome.dev.mode.plist
tada!
For all y'all on
MacOS
... setup a simple LaunchAgent to enable these glamorous capabilities in your own copy of Chrome...Save a
plist
, named whatever (launch.chrome.dev.mode.plist
, for example) in~/Library/LaunchAgents
with similar content to...It should launch at startup.. but you can force it to do so at any time with the terminal command
launchctl load -w ~/Library/LaunchAgents/launch.chrome.dev.mode.plist
TADA! ???? ???????? ???? ????????
嗯。我刚刚发现了一些官方单词“试图加载使用Dojo/文本插件的未构建的远程AMD模块,由于跨原始安全性限制而导致失败。构建系统。)“
er. I just found some official words "Attempting to load unbuilt, remote AMD modules that use the dojo/text plugin will fail due to cross-origin security restrictions. (Built versions of AMD modules are unaffected because the calls to dojo/text are eliminated by the build system.)" https://dojotoolkit.org/documentation/tutorials/1.10/cdn/
加载本地文件的一种方法是在项目文件夹中使用它们,而不是在项目文件夹外部使用。在您的项目示例文件下创建一个文件夹,类似于我们为图像创建的方式,并替换了使用项目路径以外的完整路径并使用项目文件夹下的文件相对URL的部分。
它对我有用
One way it worked loading local files is using them with in the project folder instead of outside your project folder. Create one folder under your project example files similar to the way we create for images and replace the section where using complete local path other than project path and use relative url of file under project folder .
It worked for me
Start the app server,and you should be able to access the file from localhost
对于Linux Python用户:
For Linux Python users:
为此,我的问题缺少'/'示例:
jQuery-1.10.2.js:8720 xmlhttprequest不能加载 http:// localhost:xxx/product/getlist_taglabels/
我希望我希望这对谁有帮助解决这个问题。
Many problem for this, with my problem is missing '/' example:
jquery-1.10.2.js:8720 XMLHttpRequest cannot load http://localhost:xxxProduct/getList_tagLabels/
It's must be: http://localhost:xxx/Product/getList_tagLabels/
I hope this help for who meet this problem.
使用以下HREF,我还可以重新创建此错误消息:
在我的情况下,正在使用一个标签来获取“指针光标”,并且该事件实际上是由点击事件的一些jQuery控制的。我删除了HREF并添加了适用的类:
I have also been able to recreate this error message when using an anchor tag with the following href:
In my case an a tag was being used to get the 'Pointer Cursor' and the event was actually controlled by some jQuery on click event. I removed the href and added a class that applies:
科尔多瓦实现了这一点。我仍然不知道科尔多瓦的表现如何。它甚至没有通过Enewernceptrequest。
后来我发现从本地加载任何文件的关键是:mywebview.getSettings()。setallowuniversalaccessalaccessfromfileurls(true);
当您要访问任何HTTP资源时,WebView将使用选项方法进行检查,您可以通过webViewClient.shoundinderceptrequest通过返回响应来授予访问权限,并且对于以下get/post方法,您只需返回null。
cordova achieve this. I still can not figure out how cordova did. It does not even go through shouldInterceptRequest.
Later I found out that the key to load any file from local is: myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
And when you want to access any http resource, the webview will do checking with OPTIONS method, which you can grant the access through WebViewClient.shouldInterceptRequest by return a response, and for the following GET/POST method, you can just return null.
如果要搜索firebase托管的解决方案,则可以
从 firebase cli
这就是我来这里的目的,所以我想我只是把它留在这里以帮助喜欢。
If you are searching for a solution for Firebase Hosting, you can run the
firebase serve --only hosting
command from the Firebase CLIThat's what I came here for, so I thought I'd just leave it here to help like ones.
如果您使用的VS代码只是尝试在其中加载实时服务器。立即解决了我的问题。
If your using VS code just trying loading a live server in there. fixed my problem immediately.
URL应该像:
而不是:
url should be like:
instead of: