10

I have variable $scope.data= [{column:"age", operator: ">", value: "50"}, {column:"name", operator: "=", value: "Tonda"}]. And service for submitting data to server:

angular.module('myServices', ['ngResource']).
  factory('serverApp', function($resource, $scope){
    return $resource('myurl/', {}, {
        saveData: {method:'POST', params: $scope.data}
    });
});

Why URL contains "nonsense" after calling `serverApp.saveData()? - .../myurl?0=%5Bobject+Object%5D&1=%5Bobject+Object%5D - It seems, that params can be only simple (1D) object.

How can I properly serialize object $scope.cfgcondition into params of service serverApp (eg. to URL)? Thanks.

2
  • Looks like it's calling a GET for some reason. As the params are in the URL rather than the body. Commented Aug 17, 2012 at 13:11
  • Probably need to see more code. Like where you're calling it from. Commented Aug 17, 2012 at 13:12

1 Answer 1

18

The 'params' attribute defines URL query params, which I assume is intended behavior. If it was just a simple object, not an array, then you could just use $save something like

var MyRequest = $resource('/notreally'); 
$scope.data = new MyRequest;
// get stuff into $scope.data
$scope.doSubmit = function() { $scope.data.$save(); }

To post an array you need to define your own action and pass the data in as second parameter.

$scope.data= [{column:"age", operator: ">", value: "50"}, 
              {column:"name", operator: "=", value: "Tonda"}]; 
var MyRequest = $resource('/notreally', {}, {saveData: {method:'POST', isArray: true}}); 
$scope.doSubmit = function() { MyRequest.saveData({}, $scope.data);

http://docs.angularjs.org/api/ngResource.$resource https://docs.angularjs.org/api/ngResource/service/$resource

*Edited to fix misstatements regarding arrays - I thought $resource could not POST arrays, but figured out that I was wrong!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.