我正在尝试使用基本的jQuery.ajax
POST请求从一个服务器调用另一个服务器上的Web服务(跨域)。
return $.ajax({
type: "POST",
url: "http://dev.hostname.com/ws/account/example1@example.com?property_id=1&custnum=123456",
dataType:"json"
});
我总是收到以下错误
响应...
XMLHttpRequest无法加载http://dev.hostname.com/ws/account/example1@example.com?property_id=1&custnum=123456。请求的资源上没有'Access-Control-Allow-Origin'标头。因此,源
http://localhost:63342
被拒绝访问。
这个Web服务是基于Java构建的Jersey Web服务,托管在Apache Tomcat/8.0.8
上。我已经尝试将请求发送为JSONP,但在处理来自Promise对象的回调时遇到了问题。那是另一个帖子...作为替代方案,我决定研究实现CORS响应解决方案。现在,我对Java编程非常陌生,不太熟悉它,请多多包涵。
<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, Last-Modified</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>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
由于我正在使用 Tomcat 8.0.8
。我本以为这会起作用,但我仍然遇到相同的错误。我错过了什么吗?
谢谢你的帮助。
更新
我正在添加从Firefox中调用服务时Firebug的标头。这是请求标头...
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control no-cache
Connection keep-alive
Content-Length 0
Host dev.hostname.com
Origin http://localhost:63342
Pragma no-cache
Referer http://localhost:63342/keurig/default.html
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
这是响应头
Content-Length 0
Content-Type text/plain
Date Thu, 21 Aug 2014 17:50:58 GMT
Server Apache-Coyote/1.1
我确实没有看到任何“Access-Control-*”头,这些头通常会在响应中出现。