首页 > 解决方案 > 当服务器不可用时如何正确处理 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);
  }

标签: javascriptangularangular7

解决方案


当您的服务器不可用时,这取决于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)因为我不确定那是什么意思。


推荐阅读