我正在执行一个函数,以便对指定路由执行 get
请求,该路由将字符串发送到后端,以便根据该字符串返回一些结果,但我收到此错误:
Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object
这是 Angular 部分:
查看:
<input type="text" data-ng-model="searchStr">
<textarea> {{responseData}} </textarea>
Ctrl:
$scope.$watch('searchStr', function (tmpStr)
{
if (!tmpStr || tmpStr.length == 0)
return 0;
// if searchStr is still the same..
// go ahead and retrieve the data
if (tmpStr === $scope.searchStr) {
Search.get({ 'search': $scope.searchStr })
.$promise.then(function(data) {
$scope.responseData = data;
})
}
});
服务:
angular.module('MyApp')
.factory('Search', function($resource) {
return $resource('/api/search/:search', {});
});
这就是我在 Node 部分的内容:
app.get('/api/search/:search', function(req, res, next) {
request.get('http://thetvdb.com/api/GetSeries.php?seriesname=' + req.params.search, function (error, response, body) {
console.log(error, response, body);
res.end(body);
});
});
我知道在后端一切都是正确的,因为如果我从 postman 发送到 http://localhost:3000/api/search/all
,其中 all
是从前端发送到该路由的假定字符串/搜索项,它将返回我需要的所有结果。所以这似乎是我在前端做错了什么。
有什么建议吗?
最佳答案
我认为您需要在您的服务上指定 isArray:false。
return $resource('/api/search/all', {}, {
'query' : {method : 'GET', isArray : false},
});
关于javascript - 使用 Angular $resource 执行获取请求时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37352563/