Enable CORS Post Request from AngularJS to Jersey

前端 未结 4 1564
失恋的感觉
失恋的感觉 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: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.

提交回复
热议问题