This blog is mainly about Java...

Tuesday, March 17, 2015

Get Location headers from response in AngularJS and Dropwizard

You have googled and found out that
 headers('Location')  
is the method to use to get the location?
But you always get undefined?
But in your developer tools you can see the Location in the header?

Perhaps you are using CORS (Cross Origin Resource Sharing)?
Enabling CORS only exposes a small number of the available headers by default and if you want more you have to expose them your self.

Use the ExposedHeaders
 // Enable CORS headers  
 FilterRegistration.Dynamic cors = environment.servlets().addFilter("CORS", CrossOriginFilter.class);  
 // Configure CORS parameters  
 cors.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");  
 cors.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");  
 cors.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "OPTIONS,GET,PUT,POST,DELETE,HEAD");  
 cors.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM, "true");  
 cors.setInitParameter(CrossOriginFilter.EXPOSED_HEADERS_PARAM, "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin,Location")
 // Add URL mapping  
 cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");  

Note the highlighted line. That did the trick!

Labels