Search code examples
javascriptjavagulpwildflyrestangular

Origin 'http://localhost:8888' is not allowed access with Gulp, Restangular, Java EE and Wildfly 8


The Restangular works fine with post() or get(), but if I want to delete something, then I got the following error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access.

I tried the solution on this site:

How to enable Cross domain requests on JAX-RS web services?

@Provider
public class CORSFilter implements ContainerResponseFilter {

    @Override
    public void filter(final ContainerRequestContext requestContext,
                       final ContainerResponseContext cres) throws IOException {

        cres.getHeaders().add("Access-Control-Allow-Origin", "*");
        cres.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
        cres.getHeaders().add("Access-Control-Allow-Credentials", "true");
        cres.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
        cres.getHeaders().add("Access-Control-Max-Age", "1209600");
    }

}

But then I got the following error:

The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:8888, *', but only one is allowed. Origin 'http://localhost:8888' is therefore not allowed access.

I don't quite understand what does this error mean and how could I fix it? Thanks!

The localhost:8888 is defined in Gulp.


Solution

  • We added these codes in our web.xml and it solves the problem:

    <init-param>
                <param-name>cors.supportedMethods</param-name>
                <param-value>GET, POST, HEAD, PUT, DELETE</param-value>
    </init-param>