【问题标题】:Ionic - app works in browser but does not work when emulated in mobileIonic - 应用程序在浏览器中工作,但在移动设备中模拟时不起作用
【发布时间】:2015-08-05 08:49:58
【问题描述】:

我试过这个代码

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";
    })
}
}])

我在后端的 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";
?>

我的 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>

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

进行了测试

离子服务

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

离子运行 --devices

应用程序在移动设备中打开,当我输入数据并点击提交时,没有任何反应,输入字段已填充的屏幕保持不变。

有人可以帮忙吗?

通过添加以下行修复了该问题:

adding cordova-plugin-whitelist 

并将以下行添加到 config.xml

<access origin="*"/>
<allow-intent href="*"/>
<allow-navigation href="*"/>

【问题讨论】:

  • 您不能同时使用 Allow-Origin: * 和 Allow-Credentials: true。

标签: php android json angularjs ionic


【解决方案1】:

我遇到了同样的问题,我使用的是 http://url.com 然后我将我的网址更改为 https://url.com 并解决了问题

【讨论】:

    【解决方案2】:

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

    怎么做:

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

      chrome://检查

    • 在您的 Android 设备上运行您的 Ionic 应用

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

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

    【讨论】:

    • 问题解决了吗?我也有同样的问题
    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多