Access to XMLHttpRequest at ‘http://localhost:8088/upload‘ from origin ‘http://localhost:8080‘ has b

时间:2025-01-20 22:45:13

Access to XMLHttpRequest at 'http://localhost:8088/upload' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

前端页面访问后端接口出现这个错误,一般是跨域问题所导致的,在后端的接口上加入@CrossOrigin注解即可

@RestController
@CrossOrigin//可以加在类上,也可以加到方法上
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/upload")
    //@CrossOrigin
    public String uploadStudent(){
        return "ok";
    }
}