我正在使用angular的$ resource来从API获取数据。我的angular $资源配置如下:
Priorities : $resource (baseUrl + 'priorities/:priorityType/:uuid/all', {}, {
query : {
method: 'GET',
params: {
priorityType : '@priorityType',
uuid : '@uuid'
},
isArray: true
}
})
但是,在调用Priorities.query时会调用$ resource:抛出badcfg错误:“包含数组但得到对象的预期响应”。此异常意味着API返回了一个对象,但$ resource配置为接收一个数组 - 但API显然返回一个数组:
[{"priority":"ONE","count":5,"globalCount":3037}]
深入了解angular-resource.js,抛出异常:
if (angular.isArray(data) !== (!!action.isArray)) {
throw $resourceMinErr('badcfg', ...);
}
按预期!!action.isArray
返回true,但奇怪的是angular.isArray(data)
返回false。这是怎么回事?
答案 0 :(得分:0)
需要从json转换响应。
class Banner extends SortableObject {
private static $db = array(
);
private static $has_one = array(
'HomePage' => 'HomePage',
'Image' => 'Image',
'SmallImage' => 'SmallImage',
'Target' => 'Page',
);
private static $summary_fields = array(
'ID',
'Target.MenuTitle',
);
public function getCMSFields() {
$fields = new FieldList();
$img = new UploadField('Image');
$img->setFolderName('hpbanners');
$img->getValidator()->setAllowedExtensions(array('jpg','gif','png'));
$fields->push($img);
$smallImage = new UploadField('SmallImage');
$smallImage->getValidator()->setAllowedExtensions(array('jpg','gif','png'));
$fields->push($smallImage);
$drop = new TreeDropdownField('TargetID','Choose page','SiteTree');
$fields->push($drop);
return $fields;
}
}