简体   繁体   English

Ember js'Access-Control-Allow-Origin'错误,在何处放置解决方案代码

[英]Ember js 'Access-Control-Allow-Origin' error , Where to place the solution code

There have been lot of discussions about CORS and 'Access-Control-Allow-Origin' error, I have tried and read(doing it for 2 days now) lots of potential solutions but my concern is where to place the solution code. 关于CORS和“ Access-Control-Allow-Origin”错误,已经进行了很多讨论,我尝试并阅读(现在进行了两天)许多潜在的解决方案,但是我关心的是将解决方案代码放在何处。 How and in which file do I need to make changes for header to give Access to all cross server calls. 我需要更改标题的方式以及在哪个文件中进行更改,以使Access可以访问所有跨服务器调用。 I am using Ember CLI, Tomcat Apache(which is getting data from database), running on Chrome, and using ember-data. 我正在使用Ember CLI,Tomcat Apache(正在从数据库获取数据),在Chrome上运行以及使用ember-data。 May be a noob question but I really am not able to get out of it, really need help. 可能是一个菜鸟问题,但我真的无法摆脱,真的需要帮助。 Thanks in Advance. 提前致谢。

UPDATE: I am running Apache Tomcat 7 via eclipse and using simple JAVA OracleJDBC request/response to get data. 更新:我正在通过Eclipse运行Apache Tomcat 7,并使用简单的JAVA OracleJDBC请求/响应来获取数据。

Basically this error is not of the Ember-Cli. 基本上,该错误不是Ember-Cli的错误。 This is what your server (tomcat in your case) is responding, when your Ember application is communicating to your server. 当Ember应用程序与服务器通信时,这就是服务器(在您的情况下为tomcat)所响应的。 I don't know what version are you using of TomCat. 我不知道您使用的是哪个版本的TomCat。 But I will illustrate to you how to fix this problem in TomCat 7. 但是,我将向您说明如何在TomCat 7中解决此问题。

Open your web.xml file. 打开您的web.xml文件。 It will be located in {apache-tomcat-directory}/conf/ . 它将位于{apache-tomcat-directory}/conf/ You can add the filter to alter the CORS behavior anywhere in the file after the web-app root of XML file. 您可以在XML文件的web-app根目录之后的文件中的任何位置添加过滤器,以更改CORS行为。 But I will suggest you to do that after the portion of Built in Filter Mappings . 但是我建议您在“ Built in Filter Mappings ”部分之后执行此操作。

The filter will look like this: 过滤器将如下所示:

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
    <param-name>cors.allowed.origins</param-name>
    <param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

After adding this you will be able to bypass this error. 添加后,您将能够绕过此错误。 But this is only recommended if you are doing it for development purposes. 但这仅在出于开发目的而执行时才建议使用。 Please read here for complete understanding of TomCat's CORS filter . 请阅读此处以全面了解TomCat的CORS过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM