跨域问题是因为浏览器因为同源策略的问题,对接口返回来的响应进行了拦截。假如我们服务端配置了允许跨域,浏览器收到了这个消息。跨域问题浏览器就不会拦截了。
只要是端口不一样就涉及到了跨域问题了
跨域问题我们可以在前端解决也可以在后端解决
后端解决的话我们只需要加一个配置类就可以了
也可以在每个controller层的类上加@CrossOrigin注解
这种跨域问题我们也可以不管(就是什么也不做),最后部署到服务器上的时候后端接口通过nginx进行映射,这种方法也可以解决。
还可以把前端的文件全部放到后端也可以解决跨域问题。
package com.southwind.layui01.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("*")
.allowCredentials(true)
.allowedMethods("GET","POST","DELETE","PUT","PATCH")
.maxAge(3600);
}
}