项目前后端分离,会报Access to XMLHttpRequest at ‘http://localhost:8081类似于这样的错误,我忘记截图了,就找了一张图
可以尝试从后端解决,在springboot项目文件src\main\java\com\example\demo下新建一个config文件夹,加上CorsConfig类,类里面写:
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
//解决前端跨域问题
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConf