AngularJS performs an OPTIONS HTTP request for a cross-origin resource

后端 未结 14 1350
生来不讨喜
生来不讨喜 2020-11-22 03:04

I\'m trying to setup AngularJS to communicate with a cross-origin resource where the asset host which delivers my template files is on a different domain and therefore the X

14条回答
  •  孤街浪徒
    2020-11-22 03:24

    If you are using Jersey for REST API's you can do as below

    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

        
            jersey.config.server.provider.classnames
            YOUR PACKAGE.CorsResponseFilter
        
    

提交回复
热议问题