在使用vue3进行get请求的时候发现报错,提示是跨域了,也就是端口不一致导致跨域
解决办法,这边是在后端springboot中解决,创建了一个类 然后实现WebMvcConfigurer 接口中的方法,可以直接把代码复制使用
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 CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3001")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.allowCredentials(true);
}
}