【问题标题】:Log subscribe error in AngularAngular中的日志订阅错误
【发布时间】:2018-01-06 14:14:52
【问题描述】:

我需要从发布请求中记录响应状态。但是,我看不到 subscribe 方法如何返回此信息。这是我的代码:

this.http.post(this.url, formData, options)
    .subscribe(
    (data) => {
        console.log('success');
        this.getData();
    },
    (error) => {
        console.log() // here I want to log error status
    });

【问题讨论】:

    标签: javascript angular error-handling response


    【解决方案1】:

    试试下面的,你需要console.log(error)

       return this.http.post(this.url, formData, options)
      .subscribe(
        resp => {
          const dataFromServer = resp.json();
          console.log(dataFromServer);
        },
        error => {
          console.log(error);
        }
      );
    

    【讨论】:

      【解决方案2】:

      要处理这个,你必须在.subscribe()的第一个回调中得到它:

      this.http.post(this.url, formData, options)
        .subscribe(
          (response) => {
            console.log(response.status);
            // other code
          });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-04
        • 1970-01-01
        • 1970-01-01
        • 2017-11-25
        • 1970-01-01
        • 2010-09-13
        相关资源
        最近更新 更多