Angular 通过响应类型:';水滴';通过角度的post请求
我通过get请求传递响应类型:“blob”。它工作得很好Angular 通过响应类型:';水滴';通过角度的post请求,angular,Angular,我通过get请求传递响应类型:“blob”。它工作得很好 import { HttpClient, HttpHeaders } from '@angular/common/http'; import { RequestOptions, Response, ResponseContentType } from '@angular/http'; let headers = new HttpHeaders(); headers = headers.set('Content-Type', 'appl
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { RequestOptions, Response, ResponseContentType } from '@angular/http';
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/pdf');
return this.http.get(url, {
headers: headers,
responseType: 'blob'
}
如何通过post请求传递相同的响应类型
我试过:
const headers = new Headers({
'Content-Type': 'application/pdf'
});
const options = new RequestOptions({headers: headers});
options.responseType = ResponseContentType.Blob;
return this.http.post(url, body, options)
但它不起作用。我收到错误消息:{headers:headers;}类型的参数不能分配给'RequestOptionsArgs'类型的参数。属性“headers”的类型不兼容。类型“Headers”不可分配给类型“Headers”
UPD
以下是我重新制作请求的评论:
const headers = new HttpHeaders({ 'Content-Type': 'application/pdf'});
return this.http.post(url, body, { headers, responseType:'blob' })
它工作得很好!非常感谢 您不需要“@angular/http”及其引用。不需要。请仅使用“@angular/common/http”
options.responseType = 'blob'
使用
参考资料:
- 论
responseType:'blob' as 'json'
为什么你认为它应该与get不同?文件上怎么说?您的
RequestOptions、Response、ResponseContentType
来自已弃用的@angular/http
模块,但您使用的是HttpClient
模块。请确保导入正确的文件。我尝试应用get请求中的逻辑,但它不起作用:post(url、正文、{headers:headers、responseType:“blob”})定义“不起作用”。请注意,顺便说一句,在get中传递内容类型头是没有用的:get请求没有内容,因此它没有内容类型。
responseType:'blob' as 'json'