Skip to main content
 首页 » 编程设计

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

2025年12月25日17oomusou

我想从 Flask 后端获取到 Angular 应用程序的 JSON 响应,但找不到错误所在。返回的 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." 
    } 
  ] 
} 

这是 Controller :
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 对象传递给 Controller ​​时,这有效:
$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 之类的数据时,它也不起作用。我确定 Controller 会被调用,但 get 函数不会以某种方式获取数据。

请您参考如下方法:

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