Javascript 当服务器不可用时,如何正确处理http post请求?

Javascript 当服务器不可用时,如何正确处理http post请求?,javascript,angular,angular7,Javascript,Angular,Angular7,当服务器不可用时,如何处理正确的http post响应 现在我发送如下请求: this.requestMethods.SendCurrentStep(this.formRegister.currentForm().index).subscribe(() => { // TODO }); // Code below is not executed father 方法是: public SendCurrentStep(step: number): Observ

当服务器不可用时,如何处理正确的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);
  }
public SendCurrentStep(步骤:编号):可观察{
返回此.http.post('https://localhost:9090/NotAvailableUrl', {
“步骤”:步骤
})
.map(res=>(res)。\u body==''?{}:res)
.接住(这个.把手错误);
}

当您的服务器不可用时,由
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)
和我认为还有
.map(res=>(res)。\u body==''?{}:res)
因为我不确定这意味着什么。

您需要从第一次订阅中抛出错误,所以第二次订阅会收到它。像这样的

public SendCurrentStep(step: number): Observable<any> {
    return this.http.post('https://localhost:9090/NotAvailableUrl', {
      'step' : step
    })
      .map(res => (<any>res)._body === '' ? {} : res)
      .catch((error) =>
    {
       Observable.throw(this.handleError(e)))
    });
  }
这就是你的意思吗?如果您的意思是如何判断服务器已关闭,而不是出现内部服务器错误或无效请求,那么您真的无法判断。您可以使用响应代码吗

this.requestMethods.SendCurrentStep(this.formRegister.currentForm().index).subscribe(() => {
       // TODO
    }).catch((error) => {
   Console.log("There was a problem sending this request");
});