angular - 后回调没有被调用
问题描述
我正在通过我的服务进行保存,并且保存良好:
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!");
不采取行动。
解决方案
从评论来看,很可能由于请求中缺少正确的响应类型而没有触发回调。
尝试以下
saveAllGameData(newData: any): Observable<any> {
const body = newData;
return this.http.post(
"" + this.apiDomain() + "/api/gameCentre",
body,
{ responseType: 'text' }
);
}
默认responseType
值为'json'
.
推荐阅读
- javascript - 如何处理来自可观察的不同类型的数据?
- spring - Vaadin 14 和 Push:在请求线程之外访问 Spring SecurityContext 和 Authentication
- javascript - 获取多个父母的特定孩子数据
- flutter - 颤动如何从 SfRadialGauge 中删除空间或填充
- database - 在 HomeController (Laravel 8) 中按类别名称获取帖子
- spring - 为什么在 Spring 中创建一直发生错误?
- angular - 自定义 ag gird 中的最后一行
- key-value - 如何将 consul 与 cloudflare 连接?
- reactjs - 如何在 Electron 和 CRA 的生产环境中使用 loadUrl 或 loadFile?
- scala - `Coder[P <: Product]`的Scala 2宏类型类派生以错误`P不带参数`结束