PhoneGap上的AngularJS REST服务



我正试图调用我的网页来获取phonegap中的数据。我已经把它放在了外部服务器(网页)上,当我在localhost上尝试时,一切都正常,但当我模拟它时,它就不正常了。

app.js

var myApp = angular.module('myApp',['ngRoute','ngResource','offiservServices']);
myApp.config(['$routeProvider',function($routeProvider) {
    $routeProvider.
    when('/',{
        controller : 'MainCtrl',
        templateUrl : 'views/login.html'
    });
}]);
var myAppServices = angular.module('myAppServices', ['ngResource']);
myAppServices.factory('User', ['$resource',
  function($resource){
    return $resource('http://[my-webPage]/backend.php', {}, {
      query: {method:'GET', isArray:false}
    });
  }]);
myApp.controller('MainCtrl',['$scope','User',function($scope,User) {
    $scope.user = User.query();
}]);

login.html

Username: {{ user.user }}
<br>
Password: {{ user.pass }}

我的网页返回了一个数组('user'=>'user','pass'=>'test'),正如我在localhost上所说,一切都很好(我在backend.php上添加了header('Access-Control-Allow-Origin: *');)。

所以在localhost上我得到Username: user Password: test,但在PhoneGap上我得到了Username: Password:

您应该使用调试器来控制应用程序中发生的事情。

有很多不同的方法来调试你的应用程序,例如:

  • Weinre:https://people.apache.org/~pmuellr/weinre/
  • 谷歌开发者工具:https://developers.google.com/chrome-developer-tools/docs/mobile-emulation

我知道这个问题很老了,但这个工具非常有效地"控制台"了cordova/phonegap中的应用程序(以及其他一切!)

您也可以使用http://jsconsole.com/

执行以下命令:

:listen

你会得到一条类似于的线路

<script src="http://jsconsole.com/remote.js?DCDB11CC-66D8-47F7-8DF1-FA69B2D49E17"></script>

因此,将这一行放在代码中,控制台的每一个输出都将在中间输出到jsconsole页面!

最新更新