0

I'm trying to consume a web service from my Ionic App using Angular Http. I'm following Josh Morony Example -> https://www.joshmorony.com/using-http-to-fetch-remote-data-from-a-server-in-ionic-2/ but I´m receiving the following error when I use an IP address in the url :

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

Here is my ts code -> 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);

  }

}

The problem is when I use IP address, if I use the url of the example: https://www.reddit.com/r/gifs/top/.json?limit=10&sort=hot works fine. Any ideas or comments. Thank you in advance!

6
  • the ip is not correct most probably it might be the elb ip , i am guessing as if you try it in post man no response so Angular is noit be blamed for Commented Sep 7, 2017 at 5:27
  • Hi Rahul, I tried in Postman and it works with GET, but I dont know why in Angular does not work ! Commented Sep 7, 2017 at 5:30
  • no reponse for me , you might be giving a diff url Commented Sep 7, 2017 at 5:32
  • Hi Rahul, you're right, by the moment this IP Address has a problem, I already reported, please try again later to see the problem, please notice that I updated the URL. Again, thank you for your comments! Commented Sep 7, 2017 at 13:02
  • Hi, I just tried with Postman and with PHP - CURL, and it works fine, I get a json object, but I still having the same error message with Ionic2 or Angular 2, anu comments ? Commented Sep 7, 2017 at 15:29

1 Answer 1

1

I tried the above url in my system . It looks like CORS is the issue for the same . CORS is not enabled for that ip address where as it is enabled for the cname.

This is the reason you might not have been able to access the data.

Error screenshot.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Rahul you're right, the problem was with the CORS. The solution, I configure a proxies on ionic.config.json file, and works for me, thank you for your tips.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.