has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested

跨域访问报错

has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested

解决办法:

在 Spring Boot 中针对跨域问题,提供了对应的注解 **@CrossOrigin**,使用方法如下:

/**
     * 搜索或者分页查找数据
     *
     * @param fileId
     * @param request
     * @return
     * @throws Exception
     */
    @CrossOrigin("*")
    @RequestMapping(value = "file/detail/id", method = RequestMethod.GET)
    public UploadFile getFileInfoById(
            @RequestParam(value = "fileId", defaultValue = "") String fileId,
            HttpServletRequest request) throws Exception {
        List<UploadFile> fileList = null;
        fileList = fileService.getFileDetailsById(fileId);
        if (fileList != null && fileList.size() > 0) {
            UploadFile uploadFile = fileList.get(0);
            uploadFile.setFileUrl(OssUtil.generateDownloadURl(ossClient,uploadFile.getFileName()));
            return fileList.get(0);
        } else {
            return null;
        }
    }

  
    展开阅读全文