【问题标题】:Post requisition failed发布申请失败
【发布时间】:2020-06-01 12:55:32
【问题描述】:

我正在尝试发出 POST 请求,将文件转换并下载为 pdf,但出现此错误: 文档.service.ts:

  download(documento: IDocumento) {
    return this.http.post(SERVER_API_URL + "api/download/", documento).subscribe((response) => {
      saveAs(response, documento.tituloDocumento + "." + "pdf");
    });
  }

文档资源

    @RequestMapping(value = "/download", method = RequestMethod.POST)
public void download(HttpServletResponse response, @RequestBody DocumentoDTO documentoDTO) throws IOException {
    File file = new File(documentoDTO.getPathorigdoc());
    response.setContentType("pdf");
    response.setHeader("Content-disposition", "attachment;" + documentoDTO.getTituloDocumento());
    response.getOutputStream().write(Files.readAllBytes(file.toPath()));
}

Access to XMLHttpRequest at 'https://autenticacao.campogrande.ms.gov.br/auth/realms/dev/protocol/openid-connect/auth?response_type=code&client_id=web_app&scope=openid%20offline_access%20profile%20email%20address%20phone%20roles%20web-origins%20jhipster&state=DooRYbdfmKCip2apn-NOTWQOqWBGkeh-AigMtoJVfcM%3D&redirect_uri=http://localhost:9000/login/oauth2/code/oidc' (redirected from 'http://localhost:9000/api/download/') from origin 'http://localhost:9000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
autenticacao.campogrande.ms.gov.br/auth/realms/dev/protocol/openid-connect/auth?response_type=code&client_id=web_app&scope=openid%20offline_access%20profile%20email%20address%20phone%20roles%20web-origins%20jhipster&state=DooRYbdfmKCip2apn-NOTWQOqWBGkeh-AigMtoJVfcM%3D&redirect_uri=http://localhost:9000/login/oauth2/code/oidc:1 Failed to load resource: net::ERR_FAILED
webpack-internal:///./node_modules/@angular/core/fesm5/core.js:6249 ERROR HttpErrorResponse

【问题讨论】:

  • 您需要在服务器上设置 CORS 以允许您的应用发出 XHR 请求
  • 你能告诉我我在哪里做的吗?
  • 在您的 API 上。我看到这个问题是因为它出现在我的 Angular 提要中。当您在这里没有任何答案时,您应该阅读 SpringBoot 中的 CORS。
  • 这似乎是一个很好的起点spring.io/guides/gs/rest-service-cors

标签: angular spring spring-boot post request


【解决方案1】:

不知道你用的是什么服务器,但是需要修改服务中的headers值(java中的一个例子)

How to handle CORS using JAX-RS with Jersey

【讨论】:

    【解决方案2】:

    能够解决,但您没有管理下载,我认为是因为或答案返回 null,我需要修复什么吗?

        @PostMapping(value = "/download")
    public void download(HttpServletResponse response, @RequestBody DocumentoDTO documentoDTO) throws IOException {
        String caminho = "";
    
        caminho += "/arquivos/leisweb" + File.separator + documentoDTO.getPathorigdoc();
    
        File file = new File(caminho);
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "attachment;" + documentoDTO.getTituloDocumento() + ".pdf");
        response.getOutputStream().write(Files.readAllBytes(file.toPath()));
    }
    
      download(documento: IDocumento) {
        return this.http.post(SERVER_API_URL + "api/download", documento).subscribe((response) => {
          saveAs(response, documento.tituloDocumento + "." + "pdf");
        });
      }
    

    返回:退出:.web.rest.DocumentoResource.download() with result = null

    【讨论】:

      猜你喜欢
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多