http get请求在我的angular7项目中不起作用

时间:2019-04-09 09:13:00

标签: javascript angular7

我尝试使用带有必须使用但无法正常工作的标头的get请求。

我尝试在邮递员中测试该API,并且该API正常工作

这是我的service.ts

 getUserProfile(){
 let url = '.....'
 const httpHeaders = new HttpHeaders();
 httpHeaders.append('api-client', 'x')
 httpHeaders.append('api-date', 'x');
 const options = {headers: httpHeaders};
 return this.http.get(url, options);
 }

这是我的app.component.ts

 this.networkService.getUserProfile().subscribe((response) => {
 console.log(response)},
(error) => {
  console.log('this is an error ', error)
});

我认为标头的问题导致我在控制台中将null作为值 check the error here

错误:

这是一个错误

 HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: 
 "Unknown Error", url: "http://95.......", ok: false, …}
 error: ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 
 0, total: 0, type: "error", …}
  headers: HttpHeaders
  headers: Map(0) {}
  lazyUpdate: null
   normalizedNames: Map(0) {}
   __proto__: Object
  message: "Http failure response for http://95......: 0 Unknown Error"
  name: "HttpErrorResponse"
  ok: false
  status: 0
  statusText: "Unknown Error"
  url: "......"
 __proto__: HttpResponseBase

1 个答案:

答案 0 :(得分:0)

尝试一下:

HttpHeaders.append()方法返回应捕获并保存的标头实例。

getUserProfile(){
   let url = '.....'
   const httpHeaders = new HttpHeaders();
   httpHeaders = httpHeaders.append('api-client', 'x')
   httpHeaders = httpHeaders.append('api-date', 'x');
   const options = {headers: httpHeaders};
   return this.http.get(url, options);
 }