使用Angular $资源执行获取请求时出错
我正在做一个函数,以执行get
请求到指定的路由,发送一个string到后端,以返回一些基于该string的结果,我得到这个错误:
错误:[$ resource:badcfg]资源configuration错误。 预期的响应包含一个数组,但有一个对象
这里是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', {}); });
这就是我在节点部分所具有的:
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); }); });
我知道,在后端一切都是正确的,因为如果我从邮递员到http://localhost:3000/api/search/all
all
从前端发送到该路由的假定的string/search项,返回我需要的所有结果。 所以这似乎是我在前端做错了事情。
有什么build议么?
我想你需要在你的服务上指定isArray:false。
return $resource('/api/search/all', {}, { 'query' : {method : 'GET', isArray : false}, });