AngularJs $ resource.query()给出TypeError:href为null

时间:2015-10-11 13:25:59

标签: angularjs angularjs-resource

我使用angular $ resource.query通过REST检索json数据。当我调用query()方法时,GET请求被发送到服务器并成功检索json数据:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/competitors{?page,size,sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "competitors" : [ {
      "name" : "sdfdfsdf",
      "tagline" : null,
      "url" : null,
      "active" : true,
      "itemId" : 4,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/competitors/4"
        }
      }
    }, {
      "name" : "aaaaa",
      "tagline" : null,
      "url" : null,
      "active" : true,
      "itemId" : 12,
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/competitors/12"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 3,
    "totalPages" : 1,
    "number" : 0
  }
}

但它会立即抛出这个js错误:

TypeError: href is null
consoleLog/<()angular.js (line 12477)
$ExceptionHandlerProvider/this.$get</<()angular.js (line 9246)
processQueue()angular.js (line 14753)
scheduleProcessQueue/<()angular.js (line 14761)
$RootScopeProvider/this.$get</Scope.prototype.$eval()angular.js (line 15989)
$RootScopeProvider/this.$get</Scope.prototype.$digest()angular.js (line 15800)
$RootScopeProvider/this.$get</Scope.prototype.$apply()angular.js (line 16097)
done()angular.js (line 10546)
completeRequest()angular.js (line 10744)
requestLoaded()angular.js (line 10685)


stackFrame.js (line 357)

以下是相关的js代码:

    $scope.Competitor = $resource("/competitors/:id", {id: "@id"});

    $scope.listItems = function () {
        var query = $scope.Competitor.query();
        query.$promise.then(
            function successCallback(data) {
                $scope.competitors = data._embedded.competitors;
            },
            function errorCallback(data) {
                console.log('error:');
                console.dir(data);
            }
        );
    };

错误发生后,执行errorCallback函数,但data的值为null。

我用angular-1.4.7和angular-1.2.27

尝试了这个

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

根据评论中的要求,以下是我的评论作为答案:

当您看到Firebug模糊了真正的错误:TypeError:href为null尝试使用其他浏览器或打开firefox开发人员工具而不是firebug。