Enable CORS Post Request from AngularJS to Jersey

前端 未结 4 1557
失恋的感觉
失恋的感觉 2020-12-05 20:17

I\'m attempting to post a JSON document from an AngularJS app to a Jersey REST service. The request fails, informing me that:

XMLHttpRequest cannot load http:/

相关标签:
4条回答
  • 2020-12-05 20:51

    I had faced similar CORS error while calling my Restful service (implemented in java - Jersey) from angularjs. To fix it I added Access-Control-Allow-Origin: * in response header. I added below :

    response.addHeader("Access-Control-Allow-Origin", "*"); 
    

    For more information you can check - http://enable-cors.org/server.html

    CORS error occurs typically when your angularjs code (web project) and webserivce code (server side project) are on different IP and port no.

    Your webservice implementation looks correct. So just to check, try running them on localhost on same port (eg. 8080). It should work there if all code is correct.

    In order to run them separately try adding Access-Control-Allow-Origin: * in webservice implementation as shown above.

    Hope this helps.

    0 讨论(0)
  • 2020-12-05 20:57

    Actually, you have other solution that does not need a filter. Adding the Access-Control-Allow-* headers to the GET request, is not enough, you have to create an OPTIONS endpoint to allow browsers do the pre-flight request, i.e.:

    @OPTIONS
    public Response corsMyResource(@HeaderParam("Access-Control-Request-Headers") String requestH) {
        ResponseBuilder rb = Response.ok();
    
        return buildResponse(rb, requestH);
    }
    

    see https://kdecherf.com/blog/2011/06/19/java-jersey-a-cors-compliant-rest-api/ for reference.

    0 讨论(0)
  • 2020-12-05 21:01

    The problem turned out to be inadequate handling of the OPTIONS request sent in pre-flight prior to the POST request with the proper cross origin headers.

    I was able to resolve the issue by downloading and implementing the CORS filter found at this page: http://software.dzhuvinov.com/cors-filter-installation.html.

    If you're experiencing a similar problem, follow the instructions and test to see that your OPTIONS request is no longer failing, and is immediately followed by your successful request.

    0 讨论(0)
  • 2020-12-05 21:04

    Best way is to add Jersey Response filter which will add the CORS headers for all the methods. You don't have to change your webservices implementation.

    I will explain for Jersey 2.x

    1) First add a ResponseFilter as shown below

    import java.io.IOException;
    
    import javax.ws.rs.container.ContainerRequestContext;
    import javax.ws.rs.container.ContainerResponseContext;
    import javax.ws.rs.container.ContainerResponseFilter;
    
    public class CorsResponseFilter implements ContainerResponseFilter {
    
    @Override
    public void filter(ContainerRequestContext requestContext,   ContainerResponseContext responseContext)
        throws IOException {
            responseContext.getHeaders().add("Access-Control-Allow-Origin","*");
            responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
    
      }
    }
    

    2) then in the web.xml , in the jersey servlet declaration add the below

        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>YOUR PACKAGE.CorsResponseFilter</param-value>
        </init-param>
    
    0 讨论(0)
提交回复
热议问题