Angular 2 http fetch远程服务器

时间:2017-09-06 22:41:31

标签: angular http ionic2

我正在尝试使用Angular Http从我的Ionic App中使用Web服务。我正在关注Josh Morony的例子 - > https://www.joshmorony.com/using-http-to-fetch-remote-data-from-a-server-in-ionic-2/但是当我在网址中使用IP地址时收到以下错误:

  

ERROR Object {_body:error,status:0,ok:false,statusText:“”,   headers:Object,type:3,url:null}

这是我的ts代码 - > home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  posts: any;
  url: string = 'http://200.33.191.161:9090/reservationsapi/31922904?idHotel=2
   ';

  constructor(public navCtrl: NavController, public http: Http) {

    this.http.get(this.url).map(res => res.json()).subscribe(data => {
      this.posts = data.data.children;
    });

    console.log(this.posts);

  }

}

问题是当我使用IP地址时,如果我使用示例的url:https://www.reddit.com/r/gifs/top/.json?limit=10&sort=hot工作正常。 任何想法或评论。提前谢谢!

1 个答案:

答案 0 :(得分:1)

我在我的系统中尝试了上面的url。看起来 CORS 是同样的问题。没有为该IP地址启用CORS,因为它为cname启用了。

这是您可能无法访问数据的原因。

错误截图。

enter image description here