我的应用程序没有响应前端给出的请求并抛出 CORS 异常

My Application is not responding to the request given by front end and throwing a CORS exception

我已经使用 dropwizard 创建了一个网络服务。

  1. 通过这个,我能够创建资源,其中我创建了一个实体来索引文档、搜索请求、通过 id 获取文档。

  2. 通过在邮递员中点击 API 我得到了想要的结果。

  3. 为了开发 Web 应用程序,我使用 ember.js.

  4. 构建了前端
  5. 但是当我向我的应用程序发送请求时,我收到 CORS 策略异常。

    • 当我通过邮递员向 http://localhost:9090 发送请求时(9090 是我的 ApplicationConnector 的端口号)然后它正在响应请求。
    • 现在,当我从来源 http://localhost:42004200 是 ember.js 的默认端口号)向 http://localhost:9090 发送请求时出现错误:

Access to XMLHttpRequest at http://localhost:9090/{indexName}/{id} >from origin http://localhost:4200 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.

那么有人可以解释一下如何解决这个问题,以及除了在 Dropwizard 中启用 CORS 之外还有其他方法吗?

您需要在 Dropwizard 中启用 CORS 或从同一主机(主机名和端口)同时提供前端和 API。否则它会被 CORS security feature 禁止,这在所有主流浏览器中都是默认启用的。 Postman 不关心 CORS。