我正在使用带有环回功能的angular7。我可以获得唯一的数据。谁能告诉我如何获取错误和响应状态。我想知道我在请求时间的响应代码是什么。
数据输出
[
{
"name": "karthik",
"lastname": "Selvakumar",
"course": "BE",
"year": "IV"
"id": 2
}
]
答案 0 :(得分:0)
您可以像这样从HttpResponse获取响应代码:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from "@angular/common/http";
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
status;
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get('https://jsonplaceholder.typicode.com/posts?page=0', {observe: 'response'})
.toPromise().then((response) => {
this.status = response.status;
})
}
}