我正在学习一些REST API。我将省略用于隐私目的的网址。要点是我正在接收字典类型的对象。字典中的第三个条目是一个类似JSON的对象数组。当我试图通过警报查看这些数据时,它说:
undefined不是对象
我不知道自己要做什么,要么把它变成可用的东西,要么在进入时格式化。
(function(){
var Info = "";
var exampleData = [{}];
var app = angular.module('transinfo', ["ui.bootstrap.modal","angularjs-dropdown-multiselect"]);
app.controller('PanelController', function($scope,$http){
$http.get("ThisURL gives data")
.then(function(response){
//var info = JSON.parse(response);
exampleData = response.data;
console.log(response.data);
console.log(exampleData);
}).catch(function(data){
});
$scope.example1model = [];
// $scope.example1data = [ {id: 1, label: "David"}, {id: 2, label: "Jhon"}, {id: 3, label: "Danny"} ];
$scope.example1data = exampleData;
alert(exampleData.payload[0]);
Info = "blah";
this.Info = Info;
this.getInfo = function(){
$http.get("this url also gives data")
.then(function(response){
//var info = JSON.parse(response);
this.Info=response.data;
console.log(response.data);
console.log(response.data["success"]);
alert(response.data["payload"][0].CityID);
}).catch(function(data){
this.Info = "Error";
});
};
this.changeInfo = function(){
this.Info = "Changed";
console.log("hey");
};
this.selectTab = function(setTab){
this.tab = setTab;
//this.getInfo();
};
this.isSelected = function(checkTab){
return this.tab === checkTab;
};
});
})();
以下是我收到的一些示例数据:
{success: true, message: "", error_code: "", payload: [{CityID: 72, DescriptionES: "Puerto Rico", DescriptionEN: "Puerto Rico"}, {CityID: 999, DescriptionES: "VACIO", DescriptionEN: "EMPTY"}]}