【问题标题】:Angular 2: Uncaught 504 Error (Gateway Timeout)Angular 2:未捕获的 504 错误(网关超时)
【发布时间】:2017-11-03 11:35:39
【问题描述】:

我的 Angular 2 应用程序中的错误处理存在问题。 当后端服务器离线时,我在控制台中收到了这个未捕获的错误:

获取 http://localhost:4200/api/public/index.php/data 504(网关超时)

我在服务中的 http.get 是这样的:

getData(): Observable<any> {

let apiUrl = `${API_URL}data`;
this.http.get(apiUrl)
  .map((res => res.json() || []))
  .subscribe(response => {
    let data = [];
    let index = 0;
    if (typeof(response.data) !== 'undefined' && response.success !== false) {
      // add index to each entry
      response.data.forEach(entry => {
        data.push({
          id: index++,
          title: entry.title,
          others: entry.others
        });
      });
      this.dataCollection = data;
      this.subject.next(data);
    }
  }, error => {
    this.subject.error("The Server could not be reached. Please wait until a connection can be established, or reload the page.");
  });

    return this.subject.asObservable();
} 

如何防止 Angular 2 将此错误发送到控制台?

【问题讨论】:

  • 我不认为你可以;即使您的应用程序没有错误地处理它,浏览器“知道”请求已失败。

标签: javascript php angular typescript angular2-http


【解决方案1】:

如果不知道您为 HTTP 请求使用的依赖项,几乎不可能确定您的问题。

我最近回答了另一个关于 TypeScript 中 HTTP(s) 请求的问题:Http Request in TypeScript

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 2011-04-08
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2016-06-19
    • 2013-05-12
    相关资源
    最近更新 更多