未捕获(处于承诺状态):状态为0的URL响应:空 [英] Uncaught (in promise): Response with status: 0 for URL: null

查看:201
本文介绍了未捕获(处于承诺状态):状态为0的URL响应:空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用ionic框架并从我创建的API中解析json,然后使用它填充我的ionic应用程序中的html.当我转到所有患者页面时,出现以下错误:

I am trying to use the ionic framework and parse json from an API I created, then populate the html in my ionic app with it. When I go to my all-patients page I get the following error:

Error: Uncaught (in promise): Response with status: 0  for URL: null
    at c (http://130.215.45.72:8102/build/polyfills.js:3:19752)
    at c (http://130.215.45.72:8102/build/polyfills.js:3:19461)
    at http://130.215.45.72:8102/build/polyfills.js:3:20233

我所有的病人.

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';
import { RestService } from '../../providers/rest-service/rest-service';

@Component({
  selector: 'page-all-patients',
  templateUrl: 'all-patients.html',
  providers: [RestService]
})
export class AllPatientsPage {
  public data: any;

  constructor(public navCtrl: NavController, public restService: RestService){
    this.loadPeople();
  }

  loadPeople(){
    this.restService.load()
    .then(data => {
      this.data = data;
    });
  }
}

rest-services.ts:

rest-services.ts:

import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';

/*
  Generated class for the RestServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/

@Injectable()
export class RestService {

  constructor(public http: Http) {
    console.log('Hello RestServiceProvider Provider');
  }

  load() {
  if (this.data) {
    // already loaded data
    return Promise.resolve(this.data);
  }

  // don't have the data yet
  return new Promise(resolve => {
    this.http.get('url')
      .map(res => res.json())
      .subscribe(data => {
         this.data = data.results;
         resolve(this.data);
       });
  });
}
}

当我转到该错误所在的页面时,它完全崩溃,但是控制台中没有任何内容打印出来.我不确定这是否是API或其他方面的数据解析错误.我已经手动转到api的页面,并且数据按预期显示.

When I go to the page that the error is on it crashes altogether, but nothing is printed out in the console. I am unsure if this is an error with the parsing of the data in the API or something else. I have go to the page for the api manually and the data shows up as expected.

推荐答案

我想黑曜石时代已经给了您可能的核心问题,但是无论如何,您都应该处理承诺拒绝. 因此,在这种情况下,您应该相应地更改代码:

I guess that obsidian age already gave you the possible core issue, but nevertheless you should handle promises rejection no matter what. So in that case you should change your code accordingly:

我所有的病人.

...
loadPeople(){
  this.restService.load()
  .then(data => {
    this.data = data;
  })
  .catch(e => console.error); // change this line according to your debugging/logging needs
}
...

rest-services.ts:

rest-services.ts:

...
import 'rxjs/add/operator/toPromise';
...
return this.http.get('url')
  .map(res => res.json())
  .subscribe(data => {
    this.data = data.results;
    return this.data
  })
  .toPromise();
...

希望这可以澄清您报告的实际错误,但也可以让您更深入地了解正在发生的情况,因为您不会丢失有关该错误的任何重要信息.

Hopefully this clarifies the actual error that you reported but also give you more insight on what is going on as you won't be loosing any important information about the error(s).

这篇关于未捕获(处于承诺状态):状态为0的URL响应:空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆