我正在尝试在 $CATALINA_BASE/conf/web.xml 文件中实现基本的 CORS 过滤器。这是我的过滤器:
<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 版本:
Server version: Apache Tomcat/7.0.56
Server built: Sep 26 2014 12:08:24
Server number: 7.0.56.0
这是我的 AJAX 请求:
function MethodOne() {
$.ajax({
type: "GET",
url: "http://localhost:8080/crossDomain",
crossDomain: true,
success: function(response) {
$('#result').html(response);
}
});
}
还有我的请求/响应 header :
Remote Address:[::1]:8080
Request URL:http://localhost:8080/crossDomain
Request Method:GET
Status Code:302 Found
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36
Response Headersview source
Date:Thu, 29 Jan 2015 15:19:00 GMT
Location:http://localhost:8080/crossDomain/
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
如有任何帮助,我们将不胜感激。我感觉我没有以正确的方式发送请求,但真的不知道。
最佳答案
如果您的 web.xml
还有其他具有调度程序细节的过滤器,例如
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
然后 CORS 过滤器没有为我启动。
然而,下面的过滤器映射起到了作用。
<filter-mapping>
<filter-name>CORS Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<dispatcher>FORWARD</dispatcher>
was the magic
希望这对你 friend 有帮助!
关于ajax - CORS 过滤器 Tomcat 7 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28218417/