在这里阅读有关您的问题CORS的信息:http://enable-cors.org/
检查这个是否有助于你的getMsg()方法:
return Response.ok(output).header("Access-Control-Allow-Origin", "*").build();
如果上述方法不起作用,请尝试将泽西岛过滤器添加到您的服务中。创建筛选器类:
package your.package;
public class CORSFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest creq, ContainerResponse cresp) {
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
return cresp;
}
}
并注册以后赢得网络.xml与:
<servlet>
<servlet-name>CORS Filter</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>your.package.CORSFilter</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CORS Filter</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
另一种解决方案是在资源中使用此代码来为浏览器提供。把它放在你有@GET的类中。
如果未完成此工作,请尝试将提供的“访问控制-允许-源”标头与访问此资源的自定义域交换。法学博士如果您从中调用它,请使用类似的东西而不是星号。
OPTIONS
@OPTIONS
@Path("/getsample")
public Response getOptions() {
return Response.ok()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, OPTIONS")
.header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With").build();
}
"*"
http://localhost::8080
("Access-Control-Allow-Origin", "http://localhost:8080")
"*"