我已经配置了Jetty的交叉原点过滤器,但我仍然得到以下错误。有人知道什么是错的,如何解决它吗?下面的错误信息是我的覆盖描述符(即补充web.xml)
错误:
Origin http://localhost:8090 is not allowed by Access-Control-Allow-Origin.
覆盖描述符:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedMethods</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedHeaders</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<filter-pattern>/*</filter-pattern>
</filter-mapping>
</web-app>
请求头
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:Origin, Content-Type, Accept
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:8090
Referer:http://localhost:8090/home
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.8 (KHTML, like Gecko) Chrome/17.0.942.0
响应头
Allow:POST,GET,OPTIONS,HEAD
Content-Length:0
Date:Wed, 30 Nov 2011 02:13:21 GMT
Server:Jetty(7.5.4.v20111024)
再见,
我也为此奋斗了一段时间,发现最后一个节点需要是:
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
不<filter-mapping>
<filter-name>cross-origin</filter-name>
<filter-pattern>/*</filter-pattern>
</filter-mapping>
这是我找到的帮助我的链接:wiki.eclipse.org/Jetty/Feature/Cross_Origin_Filter
在我更新了我的web.xml文件并重新启动jetty服务器后,我能够使用jQuery ajax调用进行跨域请求。
Rob我在对部署到GAE的web应用程序进行跨域调用时遇到了这个问题。您可以在Servlet响应中添加显式标头,如:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException
{
res.addHeader("Access-Control-Allow-Origin", "*");
...
}
还要确保在WAR的根目录下有一个crossdomain.xml策略文件,如:
<cross-domain-policy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.adobe.com/xml/schemas/PolicyFile.xsd">
<allow-access-from domain="*"/>
</cross-domain-policy>
HTH .
我在Jetty Web Server中使用ActiveMQ Ajax时遇到了同样的问题。我的问题是,允许的头字段不接受"*"形式的通配符。
为了使ActiveMQ Ajax工作,我还必须将"选项"方法添加到allowedMethods.
Cross-Origin Filter from web.xml:<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedMethods</param-name>
<param-value>GET,POST,OPTIONS,DELETE,PUT,HEAD</param-value>
</init-param>
<init-param>
<param-name>allowedHeaders</param-name>
<param-value>origin, content-type, accept, authorization</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
For me (jetty-version 8.1.5)V20120716)只有这些行在'web.xml'有帮助:
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedMethods</param-name>
<param-value>GET,POST,DELETE,PUT,HEAD</param-value>
</init-param>
<init-param>
<param-name>allowedHeaders</param-name>
<param-value>origin, content-type, accept</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我已经将日志级别更改为DEBUG,并从jetty控制台日志中获取信息(如' get,POST,DELETE,PUT,HEAD'和'origin, content-type, accept')。例如:
DEBUG CrossOriginFilter:359 -方法DELETE是允许的方法之一[GET, POST, DELETE, PUT, HEAD]19:14:28,413
DEBUG CrossOriginFilter:389 -报头[origin, content-type, accept]不在允许的报头中[*]
然后我用$检查结果。ajax ({url: anotherHost,类型:‘删除’,…})
刚刚在浪费了很多时间后打开了一个bug报告:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=384847还要注意,参数值中的通配符大多不受支持。(即。允许头)