javascript - 当服务器不可用时如何正确处理 http post 请求?
问题描述
服务器不可用时如何处理正确的http post响应?
现在我发送如下请求:
this.requestMethods.SendCurrentStep(this.formRegister.currentForm().index).subscribe(() => {
// TODO
});
// Code below is not executed father
方法是:
public SendCurrentStep(step: number): Observable<any> {
return this.http.post('https://localhost:9090/NotAvailableUrl', {
'step' : step
})
.map(res => (<any>res)._body === '' ? {} : res)
.catch(this.handleError);
}
解决方案
当您的服务器不可用时,这取决于RXJS
您。
this.requestMethods.SendCurrentStep(this.formRegister.currentForm().index)
.subscribe(
(sucessResponse) =>
{
// TODO your stuff with returned data
},
(errorResponse) =>
{
// Here if the response is falsey -i.e: code = 500 / 404 ...
}
);
您无需在应用程序的前端实现任何操作,您只需确保从后端很好地发送响应代码状态以及响应。在这里(和整个页面)查看更多信息。
PS:删除.catch(this.handleError);
and 我认为以及.map(res => (<any>res)._body === '' ? {} : res)
因为我不确定那是什么意思。
推荐阅读
- python - 使用 python 使用 Google Drive 进行 Google 身份验证
- json - Powershell 创建 pscustomobject 对象
- regex - .htaccess 拒绝点文件但允许letsencrypt
- bazel - 如何在 Bazel 中加载 local_repository 对外部依赖项的依赖项?
- python - 将类型列从字符串更改为数字时出现 Databricks 错误
- reactjs - 打字稿键入带有数据的对象键
- google-cloud-platform - Gsuite Windows 管理员和非管理员用户
- sql - 创建大小相等的随机存储桶,行不重复
- azure-blob-storage - 如何使用 PowerShell 将导出的 CSV 文件动态写入 Azure blob 容器?
- web-services - 将简体中文转换为繁体中文的Web服务?