本文介绍了Access-Control-Allow-Origin不允许使用Origin http://127.0.0.1:8888的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在尝试使用GWT客户端访问REST Web服务时遇到此问题。
我检查了chrome页面,然后从控制台收到以下错误:
XMLHttpRequest无法加载http:// localhost: 8080 / RestWeb / webresources /通用/获取。 Access-Control-Allow-Origin不允许使用Origin http://127.0.0.1:8888。
以下是我的客户端代码
<$ p $ Request.Build = new RequestBuilder(RequestBuilder.GET,
http:// localhost:8080 / RestWeb / webresources /通用/ GET);
builder.setCallback(new RequestCallback(){
$ b $ @Override
public void onResponseReceived(Request request,Response response){
Window.alert(onResponseReceived);
}
@Override
public void onError(Request request,Throwable exception){
}
});
builder.setHeader(Content-Type,
text / plain,application / json,text / xml);
builder.setHeader(Access-Control-Allow-Methods,
PUT,GET,POST,DELETE,OPTIONS);
builder.setHeader(Access-Control-Allow-Headers,Content-Type);
builder.setHeader(Access-Control-Allow-Origin,http://127.0.0.1:8888);
尝试{
builder.send();
} catch(RequestException e){
// TODO自动生成的catch块
e.printStackTrace();
code
$ b我的服务器端代码是:
@Path(generic)
@WebService
公共类GenericResource {
@Context
私有UriInfo上下文;
@Context
私有HttpServletResponse响应;
private String content =content;
$ b $ **
*创建GenericResource的新实例
* /
public GenericResource(){
}
@GET
$ b @Path(/ get)
@Produces(application / json,text / plain)
public String getXml(){
System .out.println( GET);
//response.addHeader(content,content);
返回this.content +from get method;
$ b
我尝试了不同的方式获得回答。请帮助我。
解决方案
您需要更改您的服务器代码以支持CORS。 $ b
一个选项是过滤器:
public class CorsFilter extends OncePerRequestFilter {
@覆盖
protected void doFilterInternal(HttpServletRequest请求,HttpServletResponse响应,FilterChain filterChain)
抛出ServletException,IOException {
if(request.getHeader(Access-Control-Request-Method)!= null &OPTIONS.equals(request.getMethod())){
// CORSpre-flight请求
response.addHeader(Access-Control-Allow-Origin, *);
response.addHeader(Access-Control-Allow-Methods,GET,POST,PUT,DELETE);
response.addHeader(Access-Control-Allow-Headers,Content-Type);
response.addHeader(Access-Control-Max-Age,1800); // 30分钟
}
filterChain.doFilter(request,response);
}
}
web.xml还需要添加以下内容:
< filter>
< filter-name> cors< / filter-name>
< filter-class> com.xxx.CorsFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> cors< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
I got this problem while trying to access REST web service using GWT client.I inspect the chrome page then i got the following error from console
XMLHttpRequest cannot load http://localhost:8080/RestWeb/webresources/generic/get. Origin http://127.0.0.1:8888 is not allowed by Access-Control-Allow-Origin.
Following is my client side code
public void onModuleLoad() {
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
"http://localhost:8080/RestWeb/webresources/generic/get");
builder.setCallback(new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
Window.alert("onResponseReceived");
}
@Override
public void onError(Request request, Throwable exception) {
}
});
builder.setHeader("Content-Type",
"text/plain,application/json,text/xml");
builder.setHeader("Access-Control-Allow-Methods",
"PUT, GET, POST, DELETE, OPTIONS");
builder.setHeader("Access-Control-Allow-Headers", "Content-Type");
builder.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8888");
try {
builder.send();
} catch (RequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
My server side code is :
@Path("generic")
@WebService
public class GenericResource {
@Context
private UriInfo context;
@Context
private HttpServletResponse response;
private String content = "content";
/**
* Creates a new instance of GenericResource
*/
public GenericResource() {
}
@GET
@Path("/get")
@Produces("application/json,text/plain")
public String getXml() {
System.out.println("GET");
//response.addHeader(content, content);
return this.content + " from get method";
}
}
I tried in different ways to get answer. Please help me.
解决方案
You need to change your server code to support CORS.
One option is a filter:
public class CorsFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
// CORS "pre-flight" request
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.addHeader("Access-Control-Allow-Headers", "Content-Type");
response.addHeader("Access-Control-Max-Age", "1800");//30 min
}
filterChain.doFilter(request, response);
}
}
The web.xml needs adding the following too:
<filter>
<filter-name>cors</filter-name>
<filter-class>com.xxx.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这篇关于Access-Control-Allow-Origin不允许使用Origin http://127.0.0.1:8888的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!