【问题标题】:How to read the status from response in angular http?如何从角度http的响应中读取状态?
【发布时间】:2018-12-05 14:00:18
【问题描述】:

我在从响应中读取状态代码时遇到了一些问题。

我在服务中调用api,

return this.http.get<string>( this.remoteServer + '/google.com/open', httpOptions);

在我的控制器中,

open() {
    this.openService.open(this.selected.qrCode)
    .subscribe(
      data => {
       console.log(data);
       this.toastService.showSuccess('Unlock Successfull', 'success');
      }
    );
  }

现在我想阅读http状态文本,

我从上述调用中得到的示例 http 响应是。

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: “好的”,网址: “https://google.com/open”,好的: 错误,...}

如何读取控制器中的状态文本。

请帮帮我

【问题讨论】:

标签: angular http-headers httpresponse angular-httpclient


【解决方案1】:

您可以将{ observe: 'response' } 指定为get 请求的第二个参数,它会为您提供完整的响应数据。

如果您想同时发送其他选项,例如 headersparams,只需以这种方式将所有选项汇总到一个对象中即可。

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json'}), params : myParams, observe: 'response'}

Reference

return this.http.get<string>(
    this.remoteServer + '/google.com/open', httpOptions).subscribe((data) => {
        console.log(data.status);     // staus 200
        console.log(data.statusText);  // OK
});

【讨论】:

  • 什么是配置?
  • 嗯,这是一个预期的响应类型。我只是使用了来自 angular.io 的代码。您可以在您的情况下将其用作字符串。
  • 还有一个小疑问,如果我想在 get() 中将 http 选项作为第三个参数传递,它表明它只需要 1-2 个参数,但得到了 3 个。你能解释一下吗?感谢回复
  • 您应该仅将其作为第二个参数发送,而不是与另一个属性 { observe: 'response', header : httpHeader} 一起发送。我假设您要发送标头。如果您想发送其他选项,只需将参数添加到第二个参数对象。
  • 是的,我什至想发送我设置的 httpOptions。
【解决方案2】:

我们也可以通过在我们的请求中添加 { observe: 'response' } 来获得完整的响应。

return this._http.get<any>(this.serverUrl+'categories/'+id, { observe: 'response' })

【讨论】:

    【解决方案3】:

    {observe:'response'} 选项限定符可以添加到 HttpClient 对象的 get 或 put 中。它应该作为附加的逗号分隔字段添加到您可能已定义的任何现有选项中。请参阅下面的简单 Put 语句示例,该语句经过修改以返回完整的 http 内容。这可用于您的应用程序中的标准化状态报告。

        body = this.currentDocument;
    
        let url = environment.base_url + 'api/Document/updateDocument';
    
        // For a put the options block is the third parameter. For a get this would be the second
        this.httpClient.put(url, body,
            {
                headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
                withCredentials: true,
                // Add the observe block to the existing option parameters
                observe:'response'
            })
            .subscribe(
                response => {
    
                    statusMessage = new StatusMessage();
                    statusMessage.HttpStatus = response.status;
                    statusMessage.StatusText = response.statusText;
    
                    // This example uses an EventEmitter but you could do any kind of logging here
                    this.updateStatusPanel.emit(statusMessage);
                    // The JSON body of this response can be accessed via the response.body parameter
                },
                err => {}
            );
    

    【讨论】:

      【解决方案4】:

      data.status 会给你你想要的吗?

      【讨论】:

        猜你喜欢
        • 2021-01-28
        • 2019-03-14
        • 2019-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        相关资源
        最近更新 更多