admin管理员组文章数量:1418152
I have an restful api and angularjs app. I am using $resource inside a factory to work with this api. I have a problem with one request. I POST my api to create some elements.
/api/service/thing/make-things
I need to pass in my request some data. Here is what I am doing:
$scope.someRequest = new SomeRequest(); // factory object returning an $resource
$scope.someRequest.some_field = 'abc';
$scope.someRequest.$save({someAdditionalParams:'123'}, function(values){...handling response...});
It works fine and POSTs data I want to post, but in this particular case my post response is array of objects.
[{somestuff:'123'}, {somestuff:'321'} ... ]
Angular tries to map it back to an object and throws me an error that object was expected but got an array. I tried to create a separate resource method with isArray:1, but it still failed with same kind of error.
So, my question is: how to handle this situation? Is it possible to cancel copying $save result to $resource object?
I have an restful api and angularjs app. I am using $resource inside a factory to work with this api. I have a problem with one request. I POST my api to create some elements.
/api/service/thing/make-things
I need to pass in my request some data. Here is what I am doing:
$scope.someRequest = new SomeRequest(); // factory object returning an $resource
$scope.someRequest.some_field = 'abc';
$scope.someRequest.$save({someAdditionalParams:'123'}, function(values){...handling response...});
It works fine and POSTs data I want to post, but in this particular case my post response is array of objects.
[{somestuff:'123'}, {somestuff:'321'} ... ]
Angular tries to map it back to an object and throws me an error that object was expected but got an array. I tried to create a separate resource method with isArray:1, but it still failed with same kind of error.
So, my question is: how to handle this situation? Is it possible to cancel copying $save result to $resource object?
Share Improve this question asked Sep 6, 2013 at 20:00 user2755534user27555341 Answer
Reset to default 5Using $save, it will try to map it back. You can create a new action with isArray:true that will not try to map the result back. You would of course have to manually handle the results.
var someRequest = $resource('/api/service/thing/make-things',{'create': {method:'POST', isArray:true}});
someRequest.create({some_field = 'abc',someAdditionalParams:'123'},function(data){
$scope.someRequestArray = data;
});
True RESTful architecture is supposed to return what was created, that is why $save works the way it does. Your needs are slightly different so a custom action is needed.
本文标签: javascriptAngularJShandling RESTful resource responseStack Overflow
版权声明:本文标题:javascript - AngularJS - handling RESTful $resource response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745281449a2651445.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论