MikeW
MikeW

Reputation: 4809

Proper way to call a REST service in Phonegap

I've taken an AngularJS web page that is able to call my REST service using GET, and put this code into my Android/Phonegap project and the rest service isn't called.

so this code below works standalone but not within phone application

$http.get('http://localhost:40884/api/sampleapi/get').success(function (data) {
    alert("cool");
    $scope.posts = data;
    $scope.loading = false;
})
    .error(function (request, status, error) {
        alert("err");
        console.log(data);
        $scope.error = "An Error has occured while loading posts!";
        $scope.loading = false;
    });

I've checked the manifest has

and the res/config.xml file has

    <access origin="*"/> <!-- allow local pages -->

But these haven't made a difference. Trying to trace out the request,status,error variables only retrieve a 0 for the error variable.

Any ideas about what to try here appreciated

Cheers

M

Upvotes: 2

Views: 807

Answers (1)

keune
keune

Reputation: 5795

I believe localhost is your computer, and since your phone or emulator is a different machine, you need to replace localhost with your computer's local ip address(something like 192.168.1.10). Later when you want to make it work even on different networks you need to put your api online(like a hosting)

I suggest that you assign a static local ip to your computer and use that address instead of localhost all the time.

Upvotes: 3

Related Questions