iOS simulator $http not working


#1

Hi

I am struggling to get $http to work inside a normal standalone Dummy application inside an iOS simulator.

What I did was:

  • $ ionic start Dummy
  • $ ionic plugin add org.apache.cordova.network-information
  • $ ionic platform add ios

The I amended only the DashCtrl to the following:

.controller('DashCtrl', function ($scope, $http, $ionicPlatform) {
    $ionicPlatform.ready(function() {
        console.log(navigator.connection.type);
        $http.get('http://192.168.1.10:80/file.txt')
            .success(function(data, status) {
                console.log('Success');
                console.log(data);
                console.log(status);
            })
            .error(function(data, status) {
                console.log('Error');
                console.log(data);
                console.log(status);
            });
    });
})

The when I try $ ionic run ios -l -c -s I see that the .error() is being called not .success()

The navigator.connection.type logs wifi

However when I look at the logs on 192.168.1.10 for apache I see that the file.txt was returned, status 200 with the correct amount of bytes. Not sure why the $http.success is not firing?

Some details from my setup:

$ ionic info

OS: Mac OS X Yosemite
Node Version: v0.12.0
Cordova CLI: 4.2.0
Ionic Version: 1.0.0-rc.0
Ionic CLI Version: 1.3.13
Xcode version: Xcode 6.1.1 Build version 6A2008a 
ios-sim version: 3.1.1

$ ionic plugin list

com.ionic.keyboard 1.0.4 "Keyboard"
org.apache.cordova.console 0.2.13 "Console"
org.apache.cordova.device 0.3.0 "Device"
org.apache.cordova.network-information 0.2.15 "Network Information"

Thanks for the help


#2

plz try to change the listening port, more detail here:


#3

@sarale Are you thinking it is a cors problem? The http://192.168.1.10:80/file.txt is just for testing purposes and is a local apache server on the same IP that the ionic run would bind to. If I build an android apk and install it to the device it does work. So seems to be a simulator thing?


#4
  $http({method: 'GET', url: })
     .success(function(data){
    
     })
     .error(function(){
    
     });

Try this way, I had similar problems simulating iOS, occupy this syntax and run.


#5

Thanks, changed to

$http({method: 'GET', url: 'http://192.168.1.10/file.txt'})
            .success(function(data, status) {

produced the same issue. Still calling .error()


#6

did u try the path inside your navigator, what is the result ?


#7

Why not install gapDebug? It’s free and you can verify that really happens in the simulator with plugins and errors, you maybe out there specifically error occurring.


#8

@sarale Yes I did try http://192.168.1.10/file.txt from a browser and even from Safari browser inside the emulator and the test little file does come back.

Running $ ionic run ios -l -c -s brings back the following:

Running dev server: http://192.168.1.10:8100
Running live reload server: http://192.168.1.10:35729

So the apache test server is running on the same machine as the simulator. But even if I change the http:// to anything else I still just get the .error() function being called.

@sioesi I must be honest, I am new to the cordova world, do not even know about gapDebug. Will have a look and see if I can get it installed.

Thanks for the help. Not sure what is happening here. Its such a basic thing, just trying to get $http to return a valid file form the simulator.


#9

I must just add (to be clear), if I build an android app the $http works fine. I am just having the issue inside the ios emulator and android emulator.


#10

Gabdegub is, you could have a look, I occupy for a similar mistake and helped me a lot, available for both platforms free tool I leave the link, brings a good tutorial to configure the devices and emulators here.

GapDebug

anything, that mistake will deliver the log?


#11

@sioesi Thanks for the GapDebug info. I downloaded GapDebug, ran my app with just $ ionic run ios, gapDebug then brought up the Simulator, I loaded the app from the simulator and it registered with gapDebug. In the console of gapDebug the $http worked without an issue. So it must be something with trying $ ionic run ios -l -c -s in a live environment. At least now I have a nice debug environment.


#12

Good thing you have served, however try to test your app in a real device, I had many problems with the simulator once in a physical device all problems were solved and also GapDebug helped me a lot :slight_smile: Good luck!


#13

This error is due by CORS problem of your destination server which does not support “localhost” headers.
Infact if you execute your code from a Device or from the simulator without node server (so without -l -c -s) it gonna work perfectly.