跨域问题
这里我采用了后端解决,CORS大家可以去了解一下。
前端axios代码
后端 SpringMvc 新建个类
代码如下:
package com.news.utils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import static org.springframework.web.cors.CorsConfiguration.ALL;
/**
* CORS configuration
*/
@Configuration
public class CORSConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(ALL)
.allowedMethods(ALL)
.allowedHeaders(ALL)
.allowCredentials(true);
}
};
}
}
然后再在控制类中增加@CrossOrigin注解

就能解决问题了~
本文介绍了一种通过后端配置解决跨域问题的方法。利用Spring框架的CORS配置,允许所有来源、方法和头部请求,同时支持凭证传递。此外,还在控制类中使用@CrossOrigin注解增强跨域支持。
8154

被折叠的 条评论
为什么被折叠?



