首页 > 解决方案 > Ember 应用程序已被 CORS 策略阻止

问题描述

我正在尝试使用我的 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.

标签: ember.jsember-dataember-cli

解决方案


您的 API 服务器未配置为接受来自另一个来源的 Web 应用程序的请求。一些选项:

  1. 配置您的 API 以根据浏览器发出Access-Control-Allow-Origin的请求提供标头OPTIONS以进行测试。可能是Access-Control-Allow-Origin: *您很懒惰或无法提前知道人们的来源。如果不了解 API 的详细信息,很难对此进行更具体的说明。

  2. 使用 Ember CLI 的API 代理 功能绕过对 CORS 的需求。不过,这仅适用于开发模式。在没有 Ember CLI 的开发服务器的生产环境中,您需要一个类似的解决方案。


推荐阅读