angularjscordovaios-simulatorsteroids

Angularjs + steroids: $http won't GET the url on the app but it works on a browser


I'm building a web app using Appgyver Steroids and angularjs, and this app communicates with a backend server on a different domain.

Here is the relevant code (dumb on purpose; I tried to isolate the problem):

Controller:

// App.
var app = angular.module('app', []);

// signup controller.
app.controller('SignupCtrl', function($scope, $http) {
    $scope.signup = function() {
        $http({
            method: 'GET',
            url: 'http://localhost:8080/watch/abcde/eta',
            responseType: 'json'
        }).success(function(data, status, headers, config) {
            console.log('Success!');
        }).error(function(data, status, headers, config) {
            console.log(status);
        });
    };
});

View:

<div ng-app="app" ng-controller="SignupCtrl">
  <h1>Test</h1>
  <button ng-click="signup()">Click Me!</button>
</div>

I set up the server properly so that it is CORS enabled. I tested the code in Chrome and Safari and it is working properly: I got the right JSON response from the server.

However, when the code is encapsulated in the steroids app and run in the iOS Simulator, it won't make a request to the server. I get a status code equals to 0 and I can't see any sign of a request in the packet analyser I've set up, nor on the logs of my server (running on localhost, like the app). I haven't tried on a real device yet.

Note that AngularJS seems properly configured because I'm using it on other parts of the app and it is working properly inside the iOS Simulator (albeit these other controllers don't do any HTTP call).

Any idea of what could be wrong?

EDIT

Thanks to Marcin's answer, I realised that iOS Simulator is running on a VM, meaning that calling localhost inside it does not make sense. I used 127.0.0.1:8080 instead and it works properly.


Solution

  • you specified the url as localhost. Use the IP of your PC where the server is turned on instead. Localhost works only locally, where the server is on.