代码之家  ›  专栏  ›  技术社区  ›  I am a Student

PhoneGap-Angularjs http服务(PHP API调用)不工作

  •  1
  • I am a Student  · 技术社区  · 7 年前

    我尝试使用 Angularjs http服务 调用我的PHP API。在浏览器中一切正常。但是,当我在我的Android手机上测试它时,它不会工作。

    我的问题:

    在Android PhoneGap应用程序中,每当我触发 Angularjs http服务 它将始终触发 function (error) 即使手机与互联网相连。

    我尝试了一下,但他们的解决方案不起作用:

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

    我还要确保电话差距 config.xml 已允许使用以下权限访问Internet:

    <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="*" />
    

    然而,它仍然不起作用。

    我的问题:

    1. Angularjs http服务是否不能在PhoneGap移动应用程序上工作,而只能在Web浏览器上工作?
    2. 除了 配置。xml ,是否需要进行任何其他配置以允许PhoneGap移动应用程序访问互联网?

    Angularjs代码:

    $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.");
     });
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   I am a Student    7 年前

    看起来实际上我只是没有安装 whitelist 插件,对于我的情况 白名单 默认情况下未安装插件。

    因为我使用的是PhoneGap,而不是像这样使用命令 cordova-plugin-whitelist@latest ,则, cordova create ,则, cordova plugin add cordova-plugin-whitelist 或等等。

    我应该使用: phonegap plugin add cordova-plugin-whitelist

    安装白名单插件后,一切正常。

    所以

    1. Angularjs http服务能够处理PhoneGap移动应用程序。
    2. 除了配置。xml,我们必须确保安装了所有必要的插件,例如白名单插件。