[英]Angular Resource: Error: $resource:badcfg
I have looked at other posts here but still can't resolve my issue. 我在这里查看了其他帖子,但仍然无法解决我的问题。
I get a Error: 我收到一个错误:
$resource:badcfg Response does not match configured parameter
$ resource:badcfg响应与配置的参数不匹配
I believe this error is caused by it returning an array rather than an object or vice versa. 我相信这个错误是由它返回一个数组而不是一个对象引起的,反之亦然。
Here's my code: 这是我的代码:
Inside my factory(I added the isArray: false but still not luck) 在我的工厂里面(我添加了isArray:false但仍然没有运气)
var task = $resource('http://localhost:5000/task/:id', {id:'@id'}, {
'get': {method:'GET', isArray: false},
});
Then in the return section of my factory 然后在我工厂的返回部分
find: function(id){
return task.get({ id: id });
}
In my Flask server it does the correct response when I load the page: 在我的Flask服务器中,它在加载页面时执行正确的响应:
127.0.0.1 - - [28/Feb/2017 14:35:13] "GET /task/1 HTTP/1.1" 200 -
However I still get the error? 但是我仍然得到错误?
Also this is what my server provides if i put it in the browser http://localhost:5000/task/1 这也是我的服务器提供的,如果我把它放在浏览器http:// localhost:5000 / task / 1
[{"completed": true, "id": 1, "ownerName": "Ryan", "task": "Test Activity", "title": "A test task"}]
Iv also tried this and get the same error: 我也试过这个并得到同样的错误:
console.log(task.get({ id: 1 }));
This error occurs when the
$resource
service expects a response that can be deserialized as an array but receives an object, or vice versa.当
$resource
服务期望响应可以作为数组反序列化但接收对象时,会发生此错误,反之亦然。 By default, all resource actions expect objects, exceptquery
which expects arrays.默认情况下,除了需要数组的
query
之外,所有资源操作都需要对象。To resolve this error, make sure your
$resource
configuration matches the actual format of the data returned from the server.要解决此错误,请确保
$resource
配置与从服务器返回的数据的实际格式相匹配。--AngularJS Error Reference - $resource:badcfg
--AngularJS错误参考 - $ resource:badcfg
The default actions for $resource are: $ resource的默认操作是:
{ 'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'} };
Avoid changing the default method: 避免更改默认方法:
var task = $resource('http://localhost:5000/task/:id', {id:'@id'}, {
//AVOID changing default
//'get': {method:'GET', isArray: true},
});
Instead use the query
action method: 而是使用
query
操作方法:
find: function(id){
//return task.get({ id: id });
//INSTEAD use query action method
return task.query({ id: id });
}
This will avoid confusing programmers that are familiar with the conventions of the $resource service. 这将避免混淆熟悉$ resource服务约定的程序员。
For more information, see AngularJS $resource API Reference . 有关更多信息,请参阅AngularJS $ resource API Reference 。
transformResponse
to Extract Object from Array transformResponse
从Array中提取对象 If the server is erroneously returning the resource object enclosed in an array, it can be extracted with a response transform function: 如果服务器错误地返回数组中包含的资源对象,则可以使用响应转换函数提取它:
var task = $resource('http://localhost:5000/task/:id', {id:'@id'}, {
'get': {method:'GET',
transformResponse: function(json) {
var data = angular.fromJson(json);
if ((typeof data == 'array' && (data.length == 1)) {
return data[0];
} else {
return data;
};
}
}
});
Have you tried with something like: 你尝试过类似的东西:
var task = $resource('http://localhost:5000/task/:id', {id:'@id'}, {
'get': {method:'GET', isArray: false},
});
You've missed {id:'@id'} to pass paramter id 您错过了{id:'@ id'}来传递参数ID
Turns out I needed to set isArray: false to true so: 原来我需要设置isArray:false为true所以:
var task = $resource('http://localhost:5000/task/:id', {id:'@id'}, {
'get': {method:'GET', isArray: true},
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.