I have an angular service which makes a call to Play server and fetches the JSON object. Below is my code snippets.
service.js
angular.module('testApp.services',[]).factory('testService',function($resource){
var service = {
getData : getData
};
function getData(){
return $resource('http://localhost:9000/testRead',{}, {
save: {
method: 'POST',
headers: {
'Auth-Token': 'B1016693LCAP8154'
}
}
});
};
return service;
});
controller.js
angular.module('testApp.controllers',['testApp.services']).controller('TestController',function($scope, testService){
testService.getData().save().$promise.then(function(data){
$scope.output = angular.fromJson(JSON.stringify(data));
console.log("Output : ", $scope.output);
});
});
In the controller, when I use testService.getData().query().$promise.
, everything works fine. But when I change it to testService.getData().save().$promise.
, it throws an error:
Error: [$resource:badcfg] Error in resource configuration
I am returning a JSON type from play. I read that save() expects an object and query() an Array type? Why is it not working then? When I check in the browser, I am getting the JSON data in the response.But in controller I am unable to use it. Please see the pics below.
Error:
Response in browser:
The error you provided seems to be not the whole log for me. So I can only make an assumption here.
By default, save method of $resource service expects the response to be a JSON object, and your response is an array of JSON objects, so it throws an error.
If it's the case, you can quickly fix that by adding isArray this to your config: (the same fix can be applied to other $resource functions such as get, remove, query)
save: {
method: 'POST',
headers: {
'Auth-Token': 'B1016693LCAP8154'
},
isArray: true
}