我在Tomcat 8.0.15上部署了一个REST API,我添加了一个CORS过滤器以激活跨域请求。
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>ACL, CANCELUPLOAD, CHECKIN, CHECKOUT, COPY, DELETE, GET, HEAD, LOCK, MKCALENDAR, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, REPORT, SEARCH, UNCHECKOUT, UNLOCK, UPDATE, VERSION-CONTROL</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>DAV, content-length, Allow</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
客户方:
function testF3(){
var user = { "login": "test", "password": "test*1" } ;
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'https://xxxxxx/login',// Use xxxx to mask the URL
crossDomain: true,
data: JSON.stringify( user ),
dataType:'text',
success: function(data)
{
console.log(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log('Fired');
console.log(errorThrown+" // "+textStatus);
}
});
}
一切正常,我在Chrome 40.0.2214.115,Opera 28.0.1750.40和Safari 5.1.7中进行了测试,并按预期得到了结果。
我使用FireFox 36.0.1做了同样的事情,但我在控制台中收到错误,指出OPTIONS请求已中止。请查看下面的图片了解更多详情:
我试图在我的web.xml中更改CORS过滤器,添加和删除支持的Headers ..但没有结果......
请非常感谢任何帮助。
非常感谢你们。