跨域问题
跨域问题
使用vue和springboot实现前后端分离项目时,前端向后端发送post请求 一直显示NETERROR
首先在后端controller使用@CrossOrign注解,未出现错误
使用apipost发送post请求,未出现错误
后经查询,分析是跨域问题,主要是由于前端项目和后端项目端口号不同导致
请求发送给了服务器,但响应被浏览器拦截,出现NETERROR错误
尝试前端解决跨域 如vue.config.js 配置 失败
最后决定在后端解决跨域问题,如下,添加CorsConfiguration类
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 CorsConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns( "*")
.allowedMethods("GET","POST","PUT","DELETE","HEAD","OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}

浙公网安备 33010602011771号