1.controller页面上加@CrossOrigin注解
2.基础页面的文件包中创建一个类,
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.WebMvcConfigurer;
/*
* 跨域解决方案
*
* */
@Configuration
public class CrosConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*")
.allowedMethods("GET","POST","PUT","DELETE","OPTIONS","HEAD").allowCredentials(true).maxAge(3600).allowedHeaders("*");
}
}
即可