使用angular获取json形式node.js / express服务器时遇到问题,可能是url路由 - (新手)

时间:2017-10-02 02:40:43

标签: angularjs json node.js express

我正在使用客户端的angular和phaser.io以及在服务器端使用express的node.js制作游戏应用程序,并且从服务器获取json已陷入僵局。

这是angularjs调用,prety简单,只需获取json并读取参数:

$scope.url = 'http://localhost:8080/game/usuario';
$scope.getInfo = function(){
             $http.get($scope.url).then(function(response) {
                $scope.mydistance = response.data.distance;
            });
         };

然后,这是服务器的返回功能。

app.get("/game/usuario", function(req,res){
console.log("JSON ACESSED!")
fs.readFile(__dirname + "/static/users.json", function (err, data) {
  if(!err) {
     res.end(data);
  } else {
     res.end(JSON.stringify(err));
  }

});

控制台日志" JSON ACCESED!"我打电话给getInfo()时从不开火,所以我猜你在网址上做错了什么。 值得注意的是,应用程序在浏览器URL http://localhost:8080/game/

上运行

并且htmls,images和js文件作为静态文件提供。

1 个答案:

答案 0 :(得分:0)

没关系。我错过了来自的http:

app.controller("control",function($scope,$interval,$http)

我觉得自己真的很蠢。