Spring Boot Angular 13请求后端java接口,跨域错误

vawmfj5a  于 2023-01-17  发布在  Spring
关注(0)|答案(1)|浏览(133)

Angular请求后端Sping Boot Application生成Excel的界面,出现跨域错误,但是我已经在后端做了跨域处理,这是怎么回事?
Access to XMLHttpRequest at 'http://localhost:8080/api/ticket/myTicketDownload' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

@CrossOrigin
    @GetMapping("/ticket/myTicketDownload")
    public void myTicketDownload(HttpServletResponse response ) throws IOException {
        response.reset();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String day = DateUtils.localDateformat(LocalDate.now());
        String fileName = URLEncoder.encode("myTicket"+day, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        TicketQueryDto ticketQueryDto = new TicketQueryDto();
        ticketQueryDto.setPkMyTicket("111");
        List<TicketVo> allTicketData = iTicketGeneralService.getMyTicketData(ticketQueryDto);
        EasyExcel.write(response.getOutputStream(), TicketVo.class).sheet(day).doWrite(allTicketData);

    }

//Angular 
ExportMyTicket(){
    return this.http.get<void>(`http://localhost:8080/api/ticket/myTicketDownload`)
    }
js81xvg6

js81xvg61#

您不能在客户端配置CORS。添加标头不会更改任何内容。您需要在后端配置它。必须在某个地方存在后端接受的域集合。

相关问题