【发布时间】: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