【问题标题】:Response for getList SHOULD be an array and not an object or something else in restangulargetList 的响应应该是一个数组,而不是一个对象或其他东西
【发布时间】:2014-03-27 07:56:19
【问题描述】:

当我在 angularJs 控制器中使用以下代码时

var  baseAccount = Restangular.all('account');
    $scope.submit = function(){      
        baseAccount.getList().then(function(accounts) {
            $scope.datas = accounts ;
        });

我收到以下错误, getList 的响应应该是一个数组,而不是一个对象或 restangular 中的其他东西

有什么解决办法吗?

【问题讨论】:

  • 服务器的响应是什么样的?
  • 从服务器端我返回了以下数据。 $names = array("name"=>"ravindra","email"=>"ravindra@gmail.com"); echo json_encode($names);
  • 在浏览器中打开开发工具并查看网络请求。查看响应的实际情况。
  • {"name":"ravindra","email":"ravindra@gmail.com"} 错误:getList 的响应应该是一个数组,而不是一个对象或其他东西。
  • 这不是一个数组。看我的回答。

标签: php jquery mysql angularjs restangular


【解决方案1】:

您正在调用getList.,它期望来自服务器的数据是一个数组(一旦它被解析为一个真正的 JS 对象)。您的响应不是数组。

您需要修复服务器端代码以响应数组或更改 Angular 代码以请求单个资源而不是它们的数组:

var baseAccount = Restangular.all('account');
$scope.submit = function () {
    baseAccount.get().then(function (account) {
        $scope.data = account; // Only one account
    });
};

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2022-10-24
  • 2011-05-30
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2012-09-30
相关资源
最近更新 更多