【问题标题】:Error performing get request with Angular $resource使用 Angular $resource 执行获取请求时出错
【发布时间】:2016-05-20 17:30:39
【问题描述】:

我正在执行一个函数,以便对指定路由执行get 请求,该路由将字符串发送到后端,以便根据该字符串返回一些结果,我收到此错误:

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

这里是 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 是从前端发送到该路由的假定字符串/搜索词,它将返回所有结果我需要。所以这似乎是我在前端做错了什么。

有什么建议吗?

【问题讨论】:

    标签: javascript angularjs node.js


    【解决方案1】:

    我认为您需要在服务中指定 isArray:false。

    return $resource('/api/search/all', {}, {
            'query' : {method : 'GET', isArray : false},
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2014-10-24
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多