首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Restler中Restler响应的误差

Restler中Restler响应的误差
EN

Stack Overflow用户
提问于 2013-07-24 13:14:03
回答 1查看 444关注 0票数 0

我已经干了几天了,似乎没办法把这件事办好。我的问题是,我在前端使用Restler (版本3)作为API和Restler,并得到以下错误:

代码语言:javascript
复制
Error: can't convert undefined to object restangularizeBase@http://localhost/vendor/restangular/src/restangular.js:436 restangularizeCollection@http://localhost/vendor/restangular/src/restangular.js:552 createServiceForConfiguration/fetchFunction/<@http://localhost/vendor/restangular/src/restangular.js:610 Qc/e/j.promise.then/h@http://localhost/vendor/angular/angular.min.js:78 Qc/g/<.then/<@http://localhost/vendor/angular/angular.min.js:78 e.prototype.$eval@http://localhost/vendor/angular/angular.min.js:88 e.prototype.$digest@http://localhost/vendor/angular/angular.min.js:86 e.prototype.$apply@http://localhost/vendor/angular/angular.min.js:88 e@http://localhost/vendor/angular/angular.min.js:95 p@http://localhost/vendor/angular/angular.min.js:98 Yc/</t.onreadystatechange@http://localhost/vendor/angular/angular.min.js:99

下面是相关的代码片段,您可以看到我在做什么:

Restler设置来设置我的API

代码语言:javascript
复制
$r = new Restler();
$r->addAPIClass('User');
$r->handle();

我将为API访问的用户类对象(现在我只是返回一个测试示例)

代码语言:javascript
复制
class User{
    public function index() {
        return  array(
            array(
                'first_name'=>'John',
                'last_name'=>'Smith',
                'role'=>'supervisor',
            ),
            array(
                'first_name'=>'Matt',
                'last_name'=>'Doe',
                'role'=>'employee',
            ),

        );
    }
}

最后我的app.js文件

代码语言:javascript
复制
'use strict';

var app = angular.module('cma',['restangular']);

app.config(function(RestangularProvider) {
    RestangularProvider.setBaseUrl('/api');
    RestangularProvider.setExtraFields(['name']);
    RestangularProvider.setResponseExtractor(function(response,operation) {
        return response.data;
    });
});

app.run(['$rootScope','Restangular',function($rootScope,Restangular) {
    var userResource = Restangular.all('session');
    $scope.test = userResource.getList(); // This is where the error is happening 
}]);

API返回以下JSON响应(取自Firebug):

代码语言:javascript
复制
GET http://localhost/api/user 200 OK 96ms

):

代码语言:javascript
复制
[
    {
        "first_name": "John",
        "last_name": "Smith",
        "role": "supervisor"
    },
    {
        "first_name": "Matt",
        "last_name": "Doe",
        "role": "employee"
    }
]

我看不出有什么事情会引起问题。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2013-08-01 21:30:00

我是赖斯特尔的创造者:)

问题是,您使用的是一个responseInterceptor,实际上是返回一个数组。

因此,您的服务器正在返回一个数组。您的responseInterceptor得到它,然后返回数组的data变量。由于这是未定义的,未定义的被发送到therefore,因此您将得到该错误。

删除responseInterceptor,一切都将开始工作:)。

凌驾

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17835112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档