Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Looks like it's calling a GET for some reason. As the params are in the URL rather than the body. –  Ben Lesh Aug 17 '12 at 13:11
    
Probably need to see more code. Like where you're calling it from. –  Ben Lesh Aug 17 '12 at 13:12

1 Answer 1

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

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

share|improve this answer
1  
Very good answer, I wish I could make it accepted. –  Sebastien F. Dec 22 '12 at 18:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.