[英]Getting a full response in Angular http Post request
I am trying to get full response from a POST request.我正在尝试从 POST 请求中获得完整响应。 I have read how to get full response for a get request mentioned at the official site of angular.
我已经阅读了如何获得 angular 官方网站上提到的获取请求的完整响应。 Angular http
角 http
What it says is to add { observe: 'response' }
.它说的是添加
{ observe: 'response' }
。 But it would work for a get
request and not for post
request.但它适用于
get
请求而不适用于post
请求。 post
request accepts 2-3 arguments, so I cannot send this as the 4th argument. post
请求接受 2-3 个参数,所以我不能将它作为第四个参数发送。 Please have a look at my code and let me know what I am doing wrong.请看看我的代码,让我知道我做错了什么。
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http.post('http://localhost:3000/api/Users/login', data, httpOptions, { observe: 'response' })
.do( function(resp) {
self.setSession(resp);
});
This gives me an error as 4 arguments are not allowed.这给了我一个错误,因为 4 个参数是不允许的。
Edit编辑
The accepted answer seems to be not working now.接受的答案现在似乎不起作用。 I am getting the following
我得到以下
error:
error TS2345: Argument of type '{ headers: HttpHeaders; observe: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
Types of property 'observe' are incompatible.
Type 'string' is not assignable to type '"body"'.
So: I needed to declare more explicitly type of httpOptions:所以:我需要更明确地声明 httpOptions 的类型:
const httpOptions: { headers; observe; } = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
observe: 'response'
};
return this.http.post('http://localhost:3000/api/Users/login', data, httpOptions) ...
observe
should be part of the httpOptions
as a property. observe
应该是httpOptions
一部分作为属性。
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
observe: 'response'
};
return this.http.post('http://localhost:3000/api/Users/login', data, httpOptions)
.do( function(resp) {
self.setSession(resp);
});
Set the options as following:设置选项如下:
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
observe: 'response' as 'response'
};
Casting 'response' as 'response' will force picking the appropriate overload.将“响应”转换为“响应”将强制选择适当的过载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.