Welcome to Qi-U Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

各位帮忙看一下这个跨域的问题如何解决,这是前端调用后端登陆接口时的错

同源策略禁止读取位于 https:///login 的远程资源。(原因:CORS 头 'Access-Control-Allow-Headers' 中的令牌 'multipart/form-data' 无效)这是火狐浏览器报的错。

has been blocked by CORS policy: Cannot parse Access-Control-Allow-Headers response header field in preflight response.这是谷歌浏览器报的错。

但其他浏览器,如360、猎豹等就可正常解析实现跨域。想问下这是怎么回事???


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

关注一下网络请求,是否发送了预检请求以及网络的响应头信息。如果确认CORS 没有开启的话,可以有后端配置下跨域设置。

如果是SpringBoot 应用的话,可以加两个Bean就可以了

@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setMaxAge(3600L);
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }
 
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Qi-U Community for programmer and developer-Open, Learning and Share
...