Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular$http get可以在浏览器中工作,但不能在设备上工作-Ionic_Javascript_Angularjs_Http_Angularjs Scope_Factory - Fatal编程技术网

Javascript Angular$http get可以在浏览器中工作,但不能在设备上工作-Ionic

Javascript Angular$http get可以在浏览器中工作,但不能在设备上工作-Ionic,javascript,angularjs,http,angularjs-scope,factory,Javascript,Angularjs,Http,Angularjs Scope,Factory,我有一个工厂返回一个调用包含JSON的内部资源的对象,如下所示: .factory('cardFactory', function ($q, $http) { return { getOtherStuff: function () { var deferred = $q.defer(), httpPromise = $http.get('/static/cards.json'); httpPromise.then(function (resp

我有一个工厂返回一个调用包含JSON的内部资源的对象,如下所示:

.factory('cardFactory', function ($q, $http) {
  return {
    getOtherStuff: function () {
      var deferred = $q.defer(),
        httpPromise = $http.get('/static/cards.json');

      httpPromise.then(function (response) {
        deferred.resolve(response);
      }, function (error) {
        console.error(error);
      });

      return deferred.promise;
    }
  };
});
在我的控制器中,我这样称呼它:

 var cardSuccess = function(data, status, headers, config) {
    $scope.cards = data.data;
  };

  cardFactory.getOtherStuff()
    .then(cardSuccess, cardError);
在浏览器中,已填充$scope.cards,但在设备上未填充

知道为什么吗?

嗯…,不知道

我在我的Ionic应用程序中使用了不同的方式,效果很好

.factory('cardFactory', function ( $http ) {
    var promise;
      var cards = {
        getOtherStuff: function() {
          if ( !promise ) {
            // $http returns a promise, which has a then function, which also returns a promise
            promise = $http.get( '/static/cards.json' ).then(function (response) {
              // The then function here is an opportunity to modify the response
              // The return value gets picked up by the then in the controller.
              return response.data;
            });
          }
          return promise; // Return the promise to the controller
        }
      };
    return cards;
 })
然后,在控制器上,通过以下方式调用它:

$scope.getData = function() {

  // Call the async method and then do stuff with what is returned inside our own then function
  cardFactory.getOtherStuff().then(function(d) {
    $scope.cards= d;
  });
}
$scope.getData()

希望能有帮助


[编辑:]可能是相对的$http.get url吗?您是否尝试使用绝对url?

将$scope.cards=data.data替换为“$scope.cards=data”


尝试删除url中$http的前导斜杠。尝试使用“static/cards.json”而不是“/static/cards.json”


我实际上也有同样的问题,这为我解决了。希望有帮助

由于它在浏览器中工作,我想这不是一个url问题,我会试试这个,让你知道,谢谢!不幸的是,它也有同样的问题。我认为我们用不同的方式实现了基本相同的东西。仍然不确定问题出在哪里。这些卡在浏览器中可以正常使用,所以这不是问题所在,不管是哪种方式,我都尝试过,不幸的是,它不起作用。我在ios上测试,但我可以在android上测试,logcat不是Java函数吗?我是用javascript写这篇文章的,不确定我能不能用itlogcat记录错误显示所有日志n事件,它的内置eclipse。()你可以试着用weinre调试你的应用。()我以前是用android开发的,所以我很熟悉logcat,因为我的应用是由ionic打包的,我不认为我能用正常的方式调试它,现在看看选项
var cardSuccess = function(data, status, headers, config) {
    $scope.cards = data;
};