Vue+SpringBoot跨域问题出现类似于Access to XMLHttpRequest at 'http://localhost:8081/的错误

出现的错误如下:
Access to XMLHttpRequest at 'http://localhost:8081/test from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

首先有两种解决方式,
1.一种是在后端进行配置
2.一种是在前端配置修改(点击查看)
这里介绍后端的局部配置和全局配置
//跨域问题的处理,全局处理方式
@Configuration
public class MyWebCrossOrgin implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/*/**")
                .allowedHeaders("*")
                .allowedMethods("*")
                .maxAge(1800)
                .allowedOrigins("http://localhost:8080");
    }
}

 

//局部处理方式

//在相对应的方法前添加如下注解

@CrossOrigin(value = "http://localhost:8080",maxAge = 1800,allowedHeaders = "*")

//具体使用

    @RequestMapping("/test")
//    @CrossOrigin(value = "http://localhost:8080",maxAge = 1800,allowedHeaders = "*")
    public Map getdata(String name, String age){
        return  userService.getdata(name,age);
    }

你可能感兴趣的:(Vue+SpringBoot跨域问题出现类似于Access to XMLHttpRequest at 'http://localhost:8081/的错误)