Ionic - 应用程序可以在浏览器中运行,但在移动设备中模拟时无法运行

Gir*_*ulu 2 php android json angularjs ionic-framework

我已经尝试过这段代码

angular.module('starter', ['ionic','ngCordova'])
.run(function($ionicPlatform) {
 $ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}
});
})
.controller('datactr',['$scope','$http',function($scope,$http) {
$scope.submit=function(){
          console.log("step1");

    $http({
      method:'POST',
      url:'http://awesomeg.2fh.co/updata.php',
      crossDomain : true,
      data:{
        'name':$scope.name,
      }

    }).success(function(data,status,header,config){
      console.log("step2");
      console.log(data);
      $scope.name="";
      $scope.message="You have successfully updated the database";
    })
}
}])
Run Code Online (Sandbox Code Playgroud)

我的后端 php 代码

<?php 
header('Access-Control-Allow-Origin: *' );
    header('Access-Control-Allow-Credentials: true' );
    header('Access-Control-Request-Method: *');
    header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE,     OPTIONS');
    header('Access-Control-Allow-Headers: *,x-requested-with,Content-Type');
    header('X-Frame-Options: DENY');
 $postdata = file_get_contents("php://input");
 $request = json_decode($postdata);
 $name=$request->name;
 if($name){
 $con=mysqli_connect('host','uname','pass','db');
 $result=mysqli_query($con,"INSERT INTO userdata (name) VALUES ('$name')");
 }
$out=json_encode($name);
echo"$out";
?>
Run Code Online (Sandbox Code Playgroud)

我的 HTML 代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>

<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
<script type="js/services.js"></script>
</head>
<body ng-app="starter">

<ion-pane>
  <ion-header-bar class="bar-stable">
    <h1 class="title">Data Collection</h1>
  </ion-header-bar>
  <ion-content ng-controller="datactr">
  <form role="form" ng-submit="submit()">
   <label class="item item-input item-stacked-label item-divider">
      Name

    <input type="text" ng-model="name" placeholder="Name">
      </label>
<div class="padding">
      <button type="submit" class="button button-block button-positive">Submit</button>
    </div>
<div class="item item-footer">
      {{message}}
    </div>
 </form>      
  </ion-content>
 </ion-pane>
 </body>
 </html>
Run Code Online (Sandbox Code Playgroud)

嗯,这是基本的离子应用程序,它从用户获取数据并将其作为 json 发送到服务器,服务器接收 json 然后处理它以在后端更新数据库。我已经在浏览器上使用它进行了测试

离子服务

当我输入数据时,它工作正常,它会更新数据库,并给出一条已成功更新的消息,但是当我尝试使用它在移动设备上测试它时

离子运行——设备

该应用程序在移动设备中打开,当我输入数据并点击提交时,没有任何反应,填充输入字段的屏幕仍然停留。

有人可以帮忙吗?

通过添加以下行解决了该问题:

adding cordova-plugin-whitelist 
Run Code Online (Sandbox Code Playgroud)

并将以下行添加到 config.xml

<access origin="*"/>
<allow-intent href="*"/>
<allow-navigation href="*"/>
Run Code Online (Sandbox Code Playgroud)

小智 5

您的代码似乎在您的设备的 webiew 中抛出异常。您可以使用 Chrome 开发者工具访问设备的控制台以及桌面 Chrome 版本中开发者工具提供的其他功能。

怎么做:

  • 将您的设备连接到计算机,
  • 在Android设备的“设置”菜单的“开发者”小程序中启用“USB调试”选项,
  • 在 Chrome 地址栏中输入以下内容:

    铬://检查

  • 在 Android 设备上运行 Ionic 应用程序

  • 在“chrome://inspect”屏幕中选择您的应用程序

您将看到带有控制台和其他开发人员工具的应用程序屏幕。您可以在那里跟踪可能的 JS 异常。