前言:坑了前端小哥好多把,这次是在一个自定义的response Headers上面
前端能够看到Header这个属性,但是获取不到
出现问题的原因W3C规定,AngularJS $http object not showing all headers from response
不能随意获取资源.
解决方案:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT")
.maxAge(3600)
.allowedHeaders("*")
/**
* 解决前端获取不到Header核心代码,不允许设置为"*
*/
.exposedHeaders(
"X-Requested-With",
"Content-Type",
"Accept",
"token",
"Origin",
"No-Cache",
"X-Requested-With",
"Captcha",
"authorization",
"Pragma",
"Last-Modified",
"Cache-Control",
"Expires",
"Authorization",
"Token",
//自定义header名称
"name");
}
网友评论