1

angular js で http リクエストを作成する際に問題があります。firebug で応答を見ることができるので、呼び出しが行われていることがわかります。

 [{"cube":"1" ,"points":"160"},{"cube":"2","points":"690"},{"cube":"3","points":"331"}] 

このチュートリアルを終了します。リンク.

したがって、私のコードは次のようになります。
私が問題を抱えているところにコメントを入れたことに注意してください。私の主な質問は、console.log が機能しないのはなぜですか? どうすれば動作させることができますか? ネットワーク要求によって返される json に $scope.usersPerCube を割り当てるにはどうすればよいですか?

var myApp = angular.module('test', []);

myApp.controller('UserCtrl', function($scope, users) {
   users.getUsers().then(function(data) {
       //not working
       $scope.usersPerCube = data;
       //this isn't getting logged
       console.log(data);
   });
   //this is getting logged and returning [object Object] which is the promise
   console.log(users.getUsers());
})

myApp.factory('users', function($http) {
   return {
     getUsers: function() {
       var url = "http://localhost/index.php/analytics/UsersPerCube"
       return $http.jsonp(url).then(function(result) {
           //this is not getting logged as well...
           console.log(result+ "logging from http");
           return result.data;
       });
     }
   }
});
4

1 に答える 1

0

JSONP メソッドを使用する必要がありますか? $http.jsonp angular.js でのJSONP $http.jsonp() 応答の解析の使用に関する次の質問を参照してください。

$http メソッドを変更するgetと、データがログに記録され、スコープ変数に正常に設定されます。

プランカー

var myApp = angular.module('test', []);
myApp.controller('UserCtrl', function($scope, users) {
   users.getUsers().then(function(data) {
       //not working
       $scope.usersPerCube = data;
       //this isn't getting logged
       console.log($scope.usersPerCube);
   });
   //this is getting logged and returning [object Object] which is the promise

});

 myApp.factory('users', function($http) {
    return {
      getUsers: function() {
        var url = "data.json?callback=jsonp_callback"
          return $http.get(url).then(function(result) {
          //this is not getting logged as well...
          console.log(result);
          return result.data;
        }, function(result){
          console.log("failed");
        });
     }
   }
 });
于 2013-11-11T18:52:50.113 に答える