我尝试使用跨域访问进行REST,但是当我尝试使用此Web服务时,我收到此错误:
XMLHttpRequest无法加载http://cx0000nt606:8087/0TesteWs/ws/carrinhos/1。对预检请求的响应没有通过访问控制检查:否'访问控制 - 允许 - 来源'标头出现在请求的资源上。起源' null'因此不允许访问。
我在java中的其他项目中尝试使用这个ws并且这样可以工作,但是在javascript上没有。
我做了一个过滤器来活跃交叉doamin,但是,不要工作.....
使用的技术:jboss-as-7.1.1.Final jdk1.7.0_79 高枕无忧 JSON
休息代码:
@Path("carrinhos")
public class ListaTudo {//implements Feature
@Path("{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String busca(@PathParam("id") Long id){
String ret = "";
Map<Long, Carrinho> mapa = null;
List<Carrinho> listaBanco = null;
if(id==0){
Gson json = new Gson();
listaBanco = new CarrinhoDAO().getListaBanco();
ret = json.toJson(listaBanco);
}else{
Carrinho carrinho = new CarrinhoDAO().busca(id);
ret = carrinho.toJson();
}
return ret;
}
@POST
@Produces(MediaType.APPLICATION_JSON)
public String adiciona(String item){
Gson json = new Gson();
Carrinho c = json.fromJson(item, Carrinho.class);
System.out.println("item: "+c);
new CarrinhoDAO().adiciona(c);
Boolean func = Boolean.TRUE;
return json.toJson(func);
}
}
jquery代码(在其他项目中):
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: "http://cx0000nt606:8087/0TesteWs/ws/carrinhos/1",
dataType: 'json',
contentType: "application/json;",
header : "token",
type: 'GET'
}).then(function(data) {
alert(data);
var atributos = {id: 'minhaDiv',class: 'minhaClasse',html: "data"};
$('<li>', atributos).appendTo('#corpo');
});
});
</script>
过滤代码:
public class CORSFilter implements ContainerResponseFilter, ContainerRequestFilter, Filter {
@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException {
response.getHeaders().add("Access-Control-Allow-Origin", "*");
response.getHeaders().add("Access-Control-Allow-Headers", "token, origin, content-type, accept, authorization");
response.getHeaders().add("Access-Control-Allow-Credentials", "true");
response.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
response.getHeaders().add("Access-Control-Max-Age", "1209600");
}
@Override
public void filter(ContainerRequestContext request) throws IOException {
request.abortWith( Response.status( Response.Status.OK ).build() );
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
((HttpServletResponse) response).addHeader("Access-Control-Allow-Origin", "*");
((HttpServletResponse) response).addHeader("Access-Control-Allow-Headers", "token, origin, content-type, accept, authorization");
((HttpServletResponse) response).addHeader("Access-Control-Allow-Credentials", "true");
((HttpServletResponse) response).addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
((HttpServletResponse) response).addHeader("Access-Control-Max-Age", "1209600");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/ws</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>restEasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>restEasy</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CORSFilter</filter-name>
<filter-class>br.com.alura.loja.webservice.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORSFilter</filter-name>
<url-pattern>/ws/*</url-pattern>
</filter-mapping>
</web-app>