我在让我的Web应用程序在Tomcat 7.0.92中工作时遇到问题。如果我调用AJAX请求,总是会收到此错误:
Quellübergreifende(Cross-Origin)Anfrage blockiert:Die Gleiche-Quelle-Regel verbietet das Lesen der externen资源http://127.0.0.1:8080/sts_v0.1/giveContent?me=haveUser&panel=addUser。 (Grund:CORS-Anschlag schlug fehl)。[Weitere Informationen]
我尝试在位于CATALINA_HOME / conf / web.xml的web.xml中使用此代码
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如Tomcat网页上所建议,但这不起作用。仍然没有可用的CORS。然后我尝试将这段代码放入web.xml
<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>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如此处有关堆栈溢出的建议。使用此配置,我的Tomcat将无法再正确启动,因为它说我不能在支持凭据的情况下完成
当allowedOrigins = [*]时,不允许配置supportsCredentials = [true]
所以Tomcat确实是从这个conf开始的,但是由于我的Web浏览器中没有Tomcat的生命迹象,因此它已“退出服务”。