Angularjs 为什么$http服务使用rest服务的完整url而$resource不使用';T
这是我的服务:Angularjs 为什么$http服务使用rest服务的完整url而$resource不使用';T,angularjs,ajax,rest,Angularjs,Ajax,Rest,这是我的服务: app.factory("ResourceService", function ($resource, $q, $http) { return { getAll: function () { var deferred = $q.defer(); $http.get('https://dog.ceo/api/breeds/list/all').then(function (response) { deferred.re
app.factory("ResourceService", function ($resource, $q, $http) {
return {
getAll: function () {
var deferred = $q.defer();
$http.get('https://dog.ceo/api/breeds/list/all').then(function (response) {
deferred.resolve(response);
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
},
allUsingResource: function () {
return $resource('https://dog.ceo/api/breeds/list/all');
}
}
});
这是我的控制器:
app.controller("Controller", function ($scope, ResourceService) {
function getAll() {
ResourceService.getAll().then(function (response) {
$scope.all = response.data;
}, function (error) {
console.log(error);
});
}
function runAll() {
var data = ResourceService.allUsingResource();
data.query(function (response) {
console.log(response);
}, function (error) {
console.log(error);
});
}
runAll();
getAll();
});
虽然使用$http一切都很顺利,但我使用$resource获得badcfg:
错误:[$resource:badcfg]$resource/badcfg?p0=query&p1=array&p2=object&p3=GET&p4=https%3A%2F%2Fdog.ceo%2Fapi%2frides%2Flist%2Fall
我缺少什么?当您运行
数据时。默认情况下,在$resource
对象上查询:
因此,请确保从https://dog.ceo/api/breeds/list/all
从为您提供的URL Angularjs读取错误:
操作查询的资源配置出错。预期响应包含数组,但获取了对象(请求:GET)
描述
当$resource服务期望一个响应可以反序列化为数组,但接收到一个对象时,或者反之亦然,就会发生此错误。默认情况下,除需要数组的查询外,所有资源操作都需要对象
要解决此错误,请确保$resource配置与从服务器返回的数据的实际格式匹配
有关更多信息,请参阅API参考文档
默认情况下,在$resource
对象上运行data.query
时,它需要:
因此,请确保从https://dog.ceo/api/breeds/list/all
从为您提供的URL Angularjs读取错误:
操作查询的资源配置出错。预期响应包含数组,但获取了对象(请求:GET)
描述
当$resource服务期望一个响应可以反序列化为数组,但接收到一个对象时,或者反之亦然,就会发生此错误。默认情况下,除需要数组的查询外,所有资源操作都需要对象
要解决此错误,请确保$resource配置与从服务器返回的数据的实际格式匹配
有关更多信息,请参阅API参考文档
使用什么样的数据结构:?使用什么样的数据结构:?这就是它不起作用的原因。可以很好地处理json数组。非常感谢这就是为什么它不起作用。可以很好地处理json数组。非常感谢
'query': {method:'GET', isArray:true},