from origin 'http://localhost:8080' has been blocked by CORS policy:

1.问题描述

 from origin 'http://localhost:8080' has been blocked by CORS policy

2.问题分析

CORS跨域访问错误

3.问题解决

编写一个配置类

package com.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


/**
 * Author:js
 * Date:2019/
 */
@Configuration
@EnableWebMvc
public class CorsConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

注意,chrome支持的跨域访问的数据类型是:http,https,data,chrome-extend,写url时不要忘记把http://头部加上。

4.参考资料

1)https://*.com/questions/35091524/spring-cors-no-access-control-allow-origin-header-is-present

上一篇:The local variable fruit may not have been initialized 错误


下一篇:One or more constraints have not been satisfied.