问题描述
我正在尝试使用我的api,并且我在ember应用程序上有一个函数可以登录,但是当触发登录操作时,我会在下面收到消息。我收到此错误的原因是什么?
login:1 Access to fetch at 'https://app-dev.some-url.com.au/api/login' from origin
'http://localhost:4099' has been blocked by CORS policy: Response to preflight request doesn't
pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested
resource. If an opaque response serves your needs,set the request's mode to 'no-cors' to fetch
the resource with CORS disabled.
解决方法
您的API服务器未配置为接受来自另一个来源的网络应用程序的请求。一些选项:
-
配置您的API以提供
Access-Control-Allow-Origin
标头 根据浏览器发出的OPTIONS
请求进行测试。它 如果您很懒或可能不会Access-Control-Allow-Origin: *
预先知道人们将来自何方。这个很难(硬 在不知道您的API详细信息的情况下对此进行更具体的说明。 -
使用Ember CLI的API proxying 功能绕过对CORS的需求。这是用于开发模式 虽然如此。您在生产中需要类似的解决方案, Ember CLI的开发服务器不存在。