【问题标题】:Angular $http get works in browser but not on device - IonicAngular $http get 在浏览器中有效,但在设备上无效 - Ionic
【发布时间】:2014-09-05 13:22:34
【问题描述】:

我有一个工厂返回一个调用包含 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,但在设备上未填充。

有什么想法吗?

【问题讨论】:

    标签: javascript angularjs http angularjs-scope factory


    【解决方案1】:

    嗯..不确定。

    我在 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 问题,我试试这个并告诉你,谢谢!
    • 不幸的是,它有同样的问题。我认为我们都以不同的方式实现了基本相同的东西。仍然不确定问题是什么。
    【解决方案2】:

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

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

    【讨论】:

    • 卡片在浏览器中运行良好,所以不是问题,无论哪种方式我都试过了,不幸的是它不起作用
    • 我在 ios 上测试,但我可以在 android 上测试,logcat 不是 Java 函数吗?我正在用 javascript 写这个,不确定我可以用它记录错误
    • logcat 显示所有记录 n 事件,它在 eclipse 中构建。(developer.android.com/tools/debugging/debugging-projects.html) 你可以尝试用 weinre 调试你的应用程序。(people.apache.org/~pmuellr/weinre-docs/latest/Home.html)
    • 我之前在android中开发过,所以对logcat很熟悉,因为我的应用程序是由ionic打包的,我认为我不能通过正常的方式对其进行调试,现在正在研究选项
    【解决方案3】:

    尝试删除 $http 网址中的前导斜杠。尝试使用 'static/cards.json' 而不是 '/static/cards.json'

    我实际上遇到了同样的问题,这为我解决了这个问题。希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2015-08-11
      • 2016-12-10
      • 2015-08-25
      • 2021-05-25
      • 2015-08-28
      • 2016-07-11
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多