0

现在我在前端使用带有 Angular 6 的 ASP.NET,但不知道发生了什么。简而言之:尽管后端发送了正确的值,但我得到了一个空的 json:

与邮递员核对

模型:

import { Car } from '../models/car';
import { Ad } from '../models/ad';

export class Owner {
    Id: number;
    Firstname: string;
    Lastname: string;
    Phone: number;
    Email: string;
    Cars: Car[];
    Ads: Ad[];
  }

服务:

      /** GET owners list */
  getOwners (): Observable<Owner[]> {
    return this.http.get<Owner[]>(this.ownersUrl)
    .pipe(
      catchError(this.handleError('getOwners', []))
    );
  }

所有者组件:

  getOwners(): void{
this.ownerService.getOwners()
    .subscribe( owners => this.owners = owners);  
}

所有者HTML:

<nb-card class="nav-card">
    <nb-card-body>
        <nb-list>
            <nb-list-item *ngFor="let owner of owners" >
                {{owner.Id}}
            </nb-list-item>
        </nb-list>
    </nb-card-body>
</nb-card>

我在做什么?服务不断给我空的 json 正文不好:/

4

1 回答 1

0

as I see it properly, how did you check that the Service returns you empty json body? Please, check the following:

  1. Your Owner class has capitalized fields, but, regarding the Postman screenshot, your server sends all lowercase fields.
  2. In Postman you send some headers, so, please, check that the data may be requested without these headers, or set these headers in your Service:

     getOwners(): Observable<Owner[]> {
    
        const httpOptions = {
          headers: new HttpHeaders({
            'Header Key':  'Header Value',
          })
        };
    
        return this.http.get<Owner[]>(this.ownersUrl)
          .pipe(
            catchError(this.handleError('getOwners', []))
          );
    }
    
于 2018-12-27T07:28:24.773 回答