【问题标题】:How to manipulate the responds in http angular如何在 http angular 中操作响应
【发布时间】:2017-10-03 23:59:30
【问题描述】:

我确实在 Angular (2) 中制作了一个应用程序,并且我需要处理 REST 服务的响应。 例如,我向一个端点发出 POST,这个端点以一个 observable 响应。在后端,如果一切顺利,端点会返回 400,但有一些自定义代码的设置。我想要的是您可以根据收到的代码使用 Angular 在 UI 中触发事件。我该如何处理?

postClient(modelClient: Client, modelAssociation?: Association) {
    const req = this.httpClient
      .post(`http://localhost:5001/api/client`, modelClient, {
        headers: this.getHeaders()
      })
      .subscribe(
      next => {
        if (modelAssociation) {
          this.postAssociation(modelAssociation);
        }
        console.log(`Se envio la Asociacion.`);
      },
      error => {
        console.log(`Ocurrio un error: ${error}`);
      },
      () => {
        console.log(`Request Complete.`);
      });
  }

这是我的服务 POST 代码。我认为将观察者的下一个属性用于用户 200 OK 代码,并将错误用于其他事情。 我的问题是我是否可以分析 POST 服务的响应?应该遵循什么策略?

【问题讨论】:

    标签: angular http http-post


    【解决方案1】:

    答案是直接使用Angular的新Http服务。它自动允许我将 Observable 的下一个操作作为响应对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多