获取API以角度代码返回带有错误代码302的HttpErrorrResponse和错误响应json wrpaped

时间:2019-01-30 07:25:29

标签: angular angular-httpclient

我编写了一个简单的GET API,该API返回一个Claim列表。 API的网址是 http://localhost:8898/claim/v1/claimByClaimNum/'+ ClaimNumber

现在,当我使用HttpClient通过我的角度代码调用它时, 我正在获取HttpErrorResponse。

我正在通过Service.ts中的以下代码调用Rest API

import { Injectable } from '@angular/core';

import { HttpClient, HttpHeaders, HttpParams} from 
'@angular/common/http';

import { ClaimResultItem } from '../claim-result/claim-result- 
item.model';

import {map} from 'rxjs/operators';

import {Observable} from 'rxjs';

@Injectable()
  export class ClaimHttpService {


 constructor(private http: HttpClient) {}

 getClaimsByClaimNumber(claimNumber: string) {

  this.http.get<ClaimResultItem> 
('http://localhost:8898/claim/v1/claimByClaimNum/' + claimNumber)
 .subscribe((response => {
   console.log(response.subjectId);
  }
   ));
}
}

当我调用上面的方法时,我得到了下面的错误-

HttpErrorResponse {headers: HttpHeaders, status: 302, statusText: "OK", 
url: "http://localhost:8898/claim/v1/claimByClaimNum/1234", ok: false, …}
error: {claimNumber: "1234", claimStatus: "Paid", claimAmount: "125.00", 
claimStartDate: "2019-01-01", claimEndDate: "2019-05-01", …}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, 
lazyInit: ƒ}
 message: "Http failure response for 
http://localhost:8898/claim/v1/claimByClaimNum/1234: 302 OK"
name: "HttpErrorResponse"
ok: false
status: 302
statusText: "OK"
 url: "http://localhost:8898/claim/v1/claimByClaimNum/1234"
 __proto__: HttpResponseBase
 constructor: ƒ HttpErrorResponse(init)
__proto__:
 constructor: ƒ HttpResponseBase(init, defaultStatus, defaultStatusText)
 __proto__: Object

似乎HttpClient正在从API获取响应,但仍然将其包装错误并以302 HTTP代码返回。

 error: {claimNumber: "1234", claimStatus: "Paid", claimAmount: "125.00", 
 claimStartDate: "2019-01-01", claimEndDate: "2019-05-01", …}

如何清除此错误,任何指针都会有所帮助。

我的Rest API可以正常工作,并且当我通过Postman独立调用它时可以给我结果。此外,REST服务还支持跨源资源。

单击按钮

,我从Onsubmit()的组件调用方法

 onSubmit() {
  console.log(this.form.value.claimNumber);
  this.claimHttpService.getClaimsByClaimNumber(this.form.value.claimNumber) 
  ;

 }

0 个答案:

没有答案