HttpErrorResponse角度7

时间:2019-02-16 02:07:13

标签: angular spring-boot

我正在使用angular 7应用程序,但是在获取请求时遇到了问题。

这是错误:

HttpErrorResponse {headers: HttpHeaders, status: 302, statusText: "OK", url: "http://localhost:9090/things/", ok: false, …}
error: Array(2)
0: {thingId: 7, thingName: "thingName 1"}
1: {thingId: 9, thingName: "thingName 2"}
length: 2
__proto__: Array(0)
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for http://localhost:9090/things/: 302 OK"
name: "HttpErrorResponse"
ok: false
status: 302
statusText: "OK"
url: "http://localhost:9090/things/"
__proto__: HttpResponseBase

这是我的RoleService.ts:

@Injectable({ providedIn: 'root' })
export default class RoleService {

  constructor(private http: HttpClient) { }

  getAllRoles(): Observable<Array<Role>> {
    return this.http.get<Array<Role>>(RoleUrls.allRoles).pipe(
      catchError(this.handleError('getRoles', []))
    )
  }

  handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {
      console.error(error); // log to console
      return of(result as T);
    };
  }
}

这是组件。

@Component({
  selector: 'app-role',
  templateUrl: './role.component.html',
  styleUrls: ['./role.component.css']
})
export class RoleComponent implements OnInit {

  roles: Array<Role>;
  constructor(private roleService: RoleService) { }

  ngOnInit() {
    this.getRoles();
  }

  getRoles(): void {
    this.roleService.getAllRoles().subscribe(roles => {
      this.roles = roles
    }, error => {
      console.log(error); // The error is here
    })
  }
}

服务器向我发送请求的数据时,我不知道此错误来自何处。

预先感谢您!

1 个答案:

答案 0 :(得分:0)

http调用总是返回一个可观察的对象。有时在尝试将响应分配给数组时会产生一些问题。尝试使用这样的作业。

 this.roles = Object.assign([], roles);