【问题标题】:Angular 2 http fetch remote serverAngular 2 http 获取远程服务器
【发布时间】:2017-09-08 14:49:38
【问题描述】:

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

ERROR Object { _body: error, status: 0, ok: false, statusText: "", 标头:对象,类型: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 工作正常。 任何想法或cmets。提前谢谢!

【问题讨论】:

  • ip 不正确,很可能是 elb ip,我猜好像你在 post man 中尝试它没有响应,所以 Angular 不应该受到责备
  • 嗨 Rahul,我在 Postman 中尝试过,它适用于 GET,但我不知道为什么在 Angular 中不起作用!
  • 没有回复我,你可能给了一个 diff url
  • 嗨 Rahul,你是对的,当这个 IP 地址有问题时,我已经报告了,请稍后再试看看问题,请注意我更新了 URL。再次感谢您的 cmets!
  • 嗨,我刚刚尝试使用 Postman 和 PHP - CURL,它工作正常,我得到了一个 json 对象,但我仍然收到与 Ionic2 或 Angular 2、anu cmets 相同的错误消息?

标签: angular http ionic2


【解决方案1】:

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

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

错误截图。

【讨论】:

  • Rahul 你是对的,问题出在 CORS 上。解决方案,我在 ionic.config.json 文件上配置了一个代理,对我有用,谢谢你的提示。
猜你喜欢
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 2016-11-08
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多