从api获取空数据

时间:2018-10-02 17:21:17

标签: json angular angular6

我正在尝试从api获取数据。尽管我可以从json占位符api和我创建的虚拟json数据中获取数据,但我绝对不能从后端api中获取数据。当我尝试从json占位符获取数据时,它会混淆数据,但是当我尝试从后端api获取数据时会显示和emty数组。而且也不是空的。

  

component.ts

allUsers: UserCreation[];

constructor(private userService: UserCreationService) { }

getUsersFromServices():void{
this.userService.getUsers().subscribe(
  (Users)=>{
    this.allUsers=Users;
    console.log(`this.allUsers = ${JSON.stringify(this.allUsers)}`);
  }
)
}

ngOnInit(): void {
this.getUsersFromServices();
}
  

service.ts

 constructor(private http:HttpClient) { }

 private usersUrl='https://jsonplaceholder.typicode.com/users';

 getUsers():Observable<UserCreation[]>{
 return this.http.get<UserCreation[]>(this.usersUrl).pipe(
  tap(receivedUsers 
  =>console.log(`receivedUsers=${JSON.stringify(receivedUsers)}`)),
  catchError(error=>of([]))
  );
 }

console when fetching json placeholder data

console while fetching api data

0 个答案:

没有答案