我已经尝试过 stackoverflow 上的其他解决方案,但没有成功。
app.services.factory('SearchResource', ['$resource', function ($resource) {
return $resource("https://..../rest/demo/v1/AMID4?auth=demo_full:1", {}, {'query': {method: 'GET', isArray: false}}); //I've also tried true...
}]);
app.services.factory('SearchService', ['$q', 'SearchResource', function($q, SearchResource) {
return {
getAllSearch: function() {
var delay = $q.defer();
SearchResource.get(
function (response) {
console.log(JSON.stringify(response));
delay.resolve(response);
},
function (response) {
delay.reject("can't get search source");
});
return delay.promise;
}
}
}]);
我正在使用上面的代码,但收到以下错误消息:
错误:[$resource:badcfg] 资源配置错误。预期响应包含一个对象,但得到一个数组
现在我已经尝试了所有其他帖子的解决方案,但我仍然无法弄清楚...当我转到网络选项卡 --> 响应(在 inspsect 元素中)时,我可以看到结果,它必须与参数有关,但我很确定我在这个阶段已经尝试了大多数格式...我对 Angular 还比较陌生,我是否遗漏了一些东西!!
最佳答案
试试这个:
app.services.factory('SearchResource', [
'$resource', function ($resource) {
return $resource("https://..../rest/demo/v1/AMID4?auth=demo_full:1", {}, {
query: { method: 'GET', isArray: true}});
}]);
然后,当您应该使用 SearchResource.query
时,您在代码中使用了 SearchResource.get
,或者您可以将其命名为 get
。
您必须定义资源提供的操作,然后使用它,如 documented here (查看页面底部的示例)。
此外,请确保您没有遗漏 $resource
定义中的任何 header 。
最后,我更喜欢使用这个(在我看来)更干净的调用:
SearchResource.query({
param_id: stuff
}).$promise.then(function(data) {
return $scope.thing = data;
}, function(e) {
return console.log(e);
});
关于javascript - AngularJS中的Web服务参数?找不到解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24700339/