-1

I want to retrieve response headers sent by server. But i am getting empty object. I can see response headers in chrome dev tools, but can't get them from javascript.

(I am using isomorphic-fetch lib for xhr requests.)

        fetch("http://my.cors.url/postsomedata", {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(myjsobject),
        })
        .then(response => response.headers)
        .then((headers) => {
            console.log(headers); // empty object
        })

This is not dublicate of How to get Response headers in AJAX I am not using jQuery.

Community
  • 1
  • 1
Vololodymyr
  • 1,996
  • 5
  • 26
  • 45

1 Answers1

0

The problem was on my back-end server CORS configuration. I am using spring boot, so in order do pass custom headers into xhr response i need to configure spring boot application in following way

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                    .exposedHeaders("MyHeader");
        }
    };
}
Vololodymyr
  • 1,996
  • 5
  • 26
  • 45