从Flask后端获取对Angular的JSON响应

时间:2013-07-15 17:55:11

标签: json angularjs flask

我想从Flask后端获取JSON响应到Angular应用程序但无法找到错误的位置。返回的JSON响应是:

{
  "phones": [
    {
    "age": 0, 
    "name": "Nexus S", 
    "snippet": "Fast just got faster with Nexus S."
    }, 
    {
    "age": 1, 
    "name": "Motorola XOOM with Wi-Fi", 
    "snippet": "The Next, Next Generation tablet."
    }, 
    {
    "age": 2, 
    "name": "MOTOROLA XOOM", 
    "snippet": "The Next, Next Generation tablet."
    }
  ]
}

这是控制器:

function Phones($scope, $http){
$http.get('127.0.0.1:5000/phones').success(function(data){
$scope.phns = data.phones;
    console.log("I'm called");
});
}

当我将JSON对象传递给控制器​​时,这会起作用:

$scope.phones = [
{"name": "Nexus S",
 "snippet": "Fast just got faster with Nexus S.",
 "age": 0},
{"name": "Motorola XOOM with Wi-Fi",
 "snippet": "The Next, Next Generation tablet.",
 "age": 1},
{"name": "MOTOROLA XOOM",
 "snippet": "The Next, Next Generation tablet.",
 "age": 2}
];

当我尝试获取data.phones等数据时,它也无效。我确定控制器被调用但get函数不能以某种方式获取数据。

1 个答案:

答案 0 :(得分:2)

将“http”添加到127.0.0.1:5000/phones应该可以解决问题。