Search code examples
androidangularjscordovaphonegapangularjs-http

PhoneGap - Angularjs http service (PHP API call) Not Working


I try to use Angularjs http service to call my PHP API. Everything working fine in Browser. But, when I test it on my Android Phone, it will not work.

My Issue:

In Android PhoneGap Application, Whenever I trigger the Angularjs http service it will always trigger the function (error) even when the Mobile is connected with Internet.

I have try look this but their solution aren't working:

  1. Angular $http.get() does not work in Phonegap DevApp
  2. Cordova 5.3.1 Android app can't access the internet

I also have ensure the Phonegap config.xml has allowed Internet Permission Access with:

<access allows-arbitrary-loads-in-media="true" allows-arbitrary-loads-in-web-content="true" allows-local-networking="true" origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />

However it is still not Working.

My Question:

  1. Is Angularjs http service is not able to work on PhoneGap Mobile Application but only Web Browser?
  2. Aside from config.xml , is there any other configuration have to be made to allow PhoneGap Mobile Application to access the Internet?

Angularjs code:

$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';

$http({
    method: 'POST',
    data: {
        'email' : $scope.email,
        'password' : $scope.password,
    },
    url: 'https://unsurfaced-cross.000webhostapp.com/PGLogin.php'
 }).then(function (response){
    $scope.users = response.data;

    if(response.data[0]=="LOGIN"){
      alert("Successful Login");
      $window.location.href = './homepage.html';

    }else{
      alert("Login Failed. Incorrect Email or Password.");
    }

 },function (error){
      alert("Please ensure You are connected to Internet.");
 });

Solution

  • seem like actually I just didn't install the whitelist plugin, for my case the whitelist plugin didn't installed by Default.

    Since I was using PhoneGap, instead of using command like cordova-plugin-whitelist@latest, cordova create, cordova plugin add cordova-plugin-whitelist or etc.

    I should use: phonegap plugin add cordova-plugin-whitelist

    After install the Whitelist Plugin, everything working fine.

    So,

    1. Angularjs http service is able to work on PhoneGap Mobile Application.
    2. Aside from config.xml , we have to ensure that all necessary and required plugin was installed such as Whitelist plugin.