AngularJS查询适用于http,但不适用于资源

时间:2014-07-13 09:57:29

标签: angularjs ngresource

我尝试使用许多答案,但它们没有用。

我正在使用api来获取AngularJS的json数据。

我可以使用$ http获取数据并将其绑定到前端。但我无法用$ resource获得相同的数据。

当我尝试使用ngResource($ resource)获取数据时,它会出现如下错误:

错误:[$ resource:badcfg]资源配置错误。预期的响应包含一个数组但得到一个对象

例如,我使用此项目,您可以从此处检查项目:https://github.com/okproject/AngularGetPost

你能帮帮我吗?

结果数据如:

{
geonames: [
{
continent: "EU",
capital: "Andorra la Vella",
languages: "ca",
geonameId: 3041565,
south: 42.42849259876837,
isoAlpha3: "AND",
north: 42.65604389629997,
fipsCode: "AN",
population: "84000",
east: 1.7865427778319827,
isoNumeric: "020",
areaInSqKm: "468.0",
countryCode: "AD",
west: 1.4071867141112762,
countryName: "Andorra",
continentName: "Europe",
currencyCode: "EUR"
},
{
continent: "AS",
capital: "Abu Dhabi",
languages: "ar-AE,fa,en,hi,ur",
geonameId: 290557,
south: 22.633329391479492,
isoAlpha3: "ARE",
north: 26.08415985107422,
fipsCode: "AE",
population: "4975593",
east: 56.38166046142578,
isoNumeric: "784",
areaInSqKm: "82880.0",
countryCode: "AE",
west: 51.58332824707031,
countryName: "United Arab Emirates",
continentName: "Asia",
currencyCode: "AED"
}
]
}

1 个答案:

答案 0 :(得分:0)

尝试,并使其正常工作,事情是您正在进行的调用是返回一个对象,而不是一个数组:

{"geonames":[{"continent":"EU","capital":"Andorra la Vella","

我通过更改资源参数(isArray:false)

来实现它
var resource = $resource('http://api.geonames.org/countryInfoJSON?username=okproject', {}, { 'query': { method: 'GET',isArray: false}});

顺便说一句,我也将用户名更改为" okproject"其余呼叫的默认帐户也失败了。

结果快照:

enter image description here

在您的控制器中,您必须更新GetEvent回调

$scope.callWithResource=function() {
    GeoService.getEvent().then(function (response) {            
        $scope.countries = response.geonames;
    });
};