Angular http.get错误,错误为{{responseType:';text}

Angular http.get错误,错误为{{responseType:';text},angular,typescript,http,rxjs,Angular,Typescript,Http,Rxjs,每当我试图在我的http.get(url)调用中使用{{requestType:'text'}}时,我会得到一个错误,我无法区分,只允许使用数组和iterables;但是,我将对象转换为数组。我需要一些帮助来理解我的错误以及如何修复我的情况 当我删除RequestType时,阵列不会出现任何问题,并显示在我的前端 ----service----- getAll(){ const requestOptions: Object = { /* other options her

每当我试图在我的http.get(url)调用中使用{{requestType:'text'}}时,我会得到一个错误,我无法区分,只允许使用数组和iterables;但是,我将对象转换为数组。我需要一些帮助来理解我的错误以及如何修复我的情况

当我删除RequestType时,阵列不会出现任何问题,并显示在我的前端

----service-----

 getAll(){
    const requestOptions: Object = {
      /* other options here */
      responseType: 'text'
    }
    return this.http.get<any>(this.url, requestOptions);
} 

---component .ts----- 

notificationsFass: any[];

  constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService) {
    this.notificationsFass = [];
}


ngOnInit() {
    this.notificationService.getAll()
      .subscribe(notificationsFass => {
        this.notificationsFass = notificationsFass;
      }
        );
}

---html---

<div *ngFor="let m of notificationsFass">

---error----
ERROR Error: Error trying to diff '[{"incidentNumber":700,"createdByName":"FASS Notification","createdDate":"2019-03-27T09:18:15.000+0000"}]'. Only arrays and iterables are allowed
——服务-----
getAll(){
常量请求选项:对象={
/*这里还有其他选择*/
响应类型:“文本”
}
返回this.http.get(this.url,requestOptions);
} 
---组件。ts------
通知类别:任何[];
构造函数(路由:ActivatedRoute,私有元服务:元,私有notificationService:NotificationsFassService){
this.notificationsFass=[];
}
恩戈尼尼特(){
this.notificationService.getAll()文件
.subscribe(notificationsFass=>{
this.notificationsFass=notificationsFass;
}
);
}
---html---
---错误----
错误:尝试区分“[{“incidentNumber”:700,“createdByName”:“FASS通知”,“createdDate”:“2019-03-27T09:18:15.000+0000”}]”时出错。只允许使用数组和iterables

根据错误消息中的json,您需要执行以下操作:

  • 定义一个接口,我使用了名称
    INotification
    。这将定义反序列化json响应上可用的成员
  • 强键入方法返回类型,并在
    http.get
    中提供泛型类型参数。调用
    http.get
    时,它将尝试将服务器的json响应反序列化为对象图。通过将
    INotification[]
    定义为返回类型,其他调用者(如来自组件的调用者)现在可以安全地调用返回类型上的成员,如
    find
    或其他Array.prototype成员,以及访问数组中实例上定义的成员
responseType:'text'
只有在您没有从服务器发出响应或者响应是文本而不是json时才有必要。前者可能发生在
post
put
delete
调用中,其中服务器可能只发送状态,而在响应中不发送消息正文

这是根据上述反馈改写的服务代码

notificationsFassService.ts

export interface INotification {
    incidentNumber: number;
    createdByName: string;
    createdDate: string;
}

export class NotificationsFassService {
    constructor (private readonly http: HttpClient) { }

    getAll():Observable<INotification[]> {
        return this.http.get<INotification[]>(this.url);
    } 
}
export class NotificationsFassComponent implements OnInit {
    notificationsFass: INotification[];

    constructor(route: ActivatedRoute, private metaService: Meta, private notificationService: NotificationsFassService) {
        this.notificationsFass = [];
    }

    ngOnInit() {
        this.notificationService.getAll()
          .subscribe(notificationsFass => {
             this.notificationsFass = notificationsFass;
          });
    }
}

从服务器发送的实际内容类型是什么?如果内容类型是
application/json
,则没有理由使用
responseType:'text'
。错误中显示的“数据”看起来是JSON,因此您可以使用默认的
responseType
,以便HttpClient解析接收到的JSON以在模板中使用。您希望使用
responseType:'text'
的原因是什么?但是,我正在将对象转换为数组。注意:在哪里?发布的代码在任何地方都不会这样做。您试图遍历一个字符串,它是application/json;然而,当我的服务器无法呈现JSON时,我收到了来自服务器的httpResponse错误。我已经读到,如果我可以得到httpResponse文本,我可能会消除这些错误。否,您从服务器获取文本响应(一个字符串)并覆盖空数组,即
notificationFass
。对,
*ngFor
只能渲染特定格式的对象,例如对象数组。如果您的内容类型是
application/json
,则绝对不要使用
responseType:'text'
。它不会被解析,只是一个字符串,在任何情况下都不能被结构指令使用,例如
*ngFor
。您可能应该分享在更改
文本
响应类型
并将其转换为数组之前获取响应的逻辑。