我已经干了几天了,似乎没办法把这件事办好。我的问题是,我在前端使用Restler (版本3)作为API和Restler,并得到以下错误:
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
$r = new Restler();
$r->addAPIClass('User');
$r->handle();我将为API访问的用户类对象(现在我只是返回一个测试示例)
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文件
'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):
GET http://localhost/api/user 200 OK 96ms):
[
{
"first_name": "John",
"last_name": "Smith",
"role": "supervisor"
},
{
"first_name": "Matt",
"last_name": "Doe",
"role": "employee"
}
]我看不出有什么事情会引起问题。任何帮助都将不胜感激!
发布于 2013-08-01 21:30:00
我是赖斯特尔的创造者:)
问题是,您使用的是一个responseInterceptor,实际上是返回一个数组。
因此,您的服务器正在返回一个数组。您的responseInterceptor得到它,然后返回数组的data变量。由于这是未定义的,未定义的被发送到therefore,因此您将得到该错误。
删除responseInterceptor,一切都将开始工作:)。
凌驾
https://stackoverflow.com/questions/17835112
复制相似问题