角度不显示从http respnse接收的对象数组

时间:2019-10-15 14:14:21

标签: angular http

它返回304而不是200,并且数组未定义

我的代码简直就是主代码。

  

app.modules.ts:

import { EmployeesModule } from './modules/employees/employees.module';
import { HttpClientModule } from '@angular/common/http';


  imports: [
    EmployeesModule,
    HttpClientModule
  ]};
  

employees.service.ts

import { HttpClient } from "@angular/common/http";

export class EmployeesService {
  employeesUrl: string = "http://localhost:3333/Employees";
  constructor(private http: HttpClient) {}

  getEmployeesData(): Observable<IEmployee[]> {
    return this.http
    .get<IEmployee[]>(this.employeesUrl);
  }
  

employees-layout.component.ts

  constructor(private employeesService: EmployeesService) { }

  ngOnInit() {
    this.getEmployees();
  }

  getEmployees(): void {
    this.employeesService.getEmployeesData()
    .subscribe(employees => this.allEmployees = employees);
  }

}

它不会加载数组,它也不为空,并且返回其未定义数据的数据。 我尝试过多次更改语法,但是没有用

1 个答案:

答案 0 :(得分:0)

如果收到的消息是302,则表明代码可能还可以,但是您调用的URL出现了一些问题。

错误302可能表示lot of things。您正在调用的后端API是什么?您写的http://localhost:3333/Employees在同一台计算机上是否有用于测试应用程序的服务器(localhost表示运行浏览器的主机)?如果您尝试呼叫different test api on the web,会发生同样的错误吗?