'XMLHttpRequest无法加载'从Dart客户端到本地Dart泊坞服务器的HTTP请求错误
我最近一直试图建立一个Dart客户端,与我的Dart泊坞服务器通信。如果我在任何浏览器上运行这个url(localhost:8080 / id / 6192449487634432),我得到一个我设置的JSON,但是,如果使用
I've lately been trying to build a Dart client that communicates with my Dart docker server. If I run this url (localhost:8080/id/6192449487634432) on any browser I get back a JSON that I've set up, however, if a use
HttpRequest
.getString ("http://localhost:8080/id/6192449487634432")
.then (print);
在Dart客户端,我得到这个奇怪的错误
on the Dart client, I get this weird error
XMLHttpRequest cannot load http://localhost:8080/id/6192449487634432. No 'Access-Control-Allow-Origin'
header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access.
我已经搜索,一些解决方法是构建一个PHP代理我是一个新的web开发一般来说,我绝对不知道什么是代理是或如何建立一个。有没有一个干净的解决方案,我可以使用?我有一个Redstone服务器和AngularDart客户端。
I've searched and some workarounds have been to build a PHP proxy (no thanks). I am new to web development in general and I definitely don't know what a proxy is or how to build one. Is there a clean solution I could use? I have a Redstone server and and AngularDart client.
非常感谢。
由与docker服务器不同的服务器提供。这是一个浏览器问题,不是Dart特有的。幸运的是,解决方案很容易。
This happens when your client app is served from a different server than your docker server. This is a browser issue, not specific to Dart. Luckily, the solution is easy.
请务必在每次请求时从您的服务器发送CORS头文件。最简单的解决方法是添加以下标题:
Be sure to send CORS headers from your server, on every request. The easiest way to solve this is to add the following header:
Access-Control-Allow-Origin:*
我不知道Redstone的具体细节,但务必设置标题键 Access-Control-Allow-Origin $ c GET
, HEAD
上的和值 *
和 POST
方法。是的,您需要在 HEAD
请求上设置此标头,因为有时浏览器会发出HEAD请求来检查是否启用CORS。
I don't know the specifics of Redstone, but be sure to set the header key Access-Control-Allow-Origin
and value *
on GET
, HEAD
, and POST
methods. Yes, you need to set this header on HEAD
requests because sometimes the browser does a HEAD request to check if CORS is enabled.
要了解有关CORS的详情,请访问 http://enable-cors.org/
Learn more about CORS at http://enable-cors.org/