首页 > 解决方案 > 后回调没有被调用

问题描述

我正在通过我的服务进行保存,并且保存良好:

this.generalService.saveAllGameData(this.gameData).subscribe(
            data => {

                console.log(data);
                
                this.deleteUnrequiredFilesOnSave();
                this.notifyService.changeNotify("Saved!");
                this.router.navigate(["/rooms"]);

            },
            error => {

            },
            () => {

                console.log("Posted!");

            }
            );

这是服务中的代码:

saveAllGameData(newData: any): Observable<any> {
        const body = newData;
        return this.http.post("" + this.apiDomain() + "/api/gameCentre", body)
    }

但是数据回调中的所有内容都不会被调用。任何想法为什么?甚至console.log("Posted!");不采取行动。

标签: angular

解决方案


从评论来看,很可能由于请求中缺少正确的响应类型而没有触发回调。

尝试以下

saveAllGameData(newData: any): Observable<any> {
  const body = newData;
  return this.http.post(
    "" + this.apiDomain() + "/api/gameCentre", 
    body, 
    { responseType: 'text' }
  );
}

默认responseType值为'json'.


推荐阅读