我正在使用带有环回的angular7。我可以获得唯一的数据。任何人都可以告诉我如何获得错误和响应状态。我想知道请求时间的响应代码是什么。
输出数据
[
{
"name": "karthik",
"lastname": "Selvakumar",
"course": "BE",
"year": "IV"
"id": 2
}
]
您可以从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;
})
}
}