我有一个component.ts文件,如下所示。
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import 'rxjs/add/operator/retry';
@Component({
selector: 'app-add-candidate-emp-referral',
templateUrl: './add-candidate-emp-referral.component.html',
styleUrls: ['./add-candidate-emp-referral.component.css']
})
export class addCandidateComponent implements ngOnInit{
url: 'http://localhost:59055/users1/1';
posts: any;
constructor(private http: HttpClient) {
}
ngOnInit() {
// this.http.get(this.url).subscribe(res => { this.posts.username = res.toString(); console.log(this.posts); console.log('hi'); });
this.http.get(this.url).retry(3).subscribe(data => { console.log(data); console.log('hi'); }, (err: HttpErrorResponse) => {
if (err.error instanceof Error) {
// A client-side or network error occurred. Handle it accordingly.
console.log('An error occurred:', err.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.log(`Backend returned code ${err.status}, body was: ${err.error},message was: ${err.message} `);
}
}
);
}
}
现在当我渲染该组件时,我无法从api url获取详细信息。但与此同时,当我使用邮递员客户端并发送网址请求时,我收到了json的响应。
答案 0 :(得分:0)
这可能归因于以下几点: