How do I send a more complex object in angular.js?

http://jsfiddle.net/EjyW4/

Essentially, I am trying to post an array of objects from the client using AngularJS with the resources module, and instead of sending a JSON object, Angular is sending a useless toString representation over the wire.

Unfortunately, the code in the fiddle itself doesn't do much -- the intent is outlined here with more context, though it still is very raw and do not yet resemble anything looking like the right way) But this seems to be an angular issue rather than grails, at least from looking at the Chrome console.

Query String Parameters:
callback:JSON_CALLBACK
tests:%5Bobject+Object%5D,%5Bobject+Object%5D

There seems to be an angular.toJson -- http://docs.angularjs.org/api/angular.toJson -- but it doesn't seem to work in this case. The documentation I've seen doesn't seem to cover more than sending a basic int. If I have to, I'll send over a comma separated string, but this seems like it should be a common use case.

The $resource function actually returns a new $resource object constructor, which you then set properties on, then call methods like save on.

So your problem in your fiddle is you're trying to save a $resource with no data set on it! All you have is a config property, tests, which it doesn't know what to do with.

You instead want to:

  1. Set up your constructor for a new resource using $resource factory/method.
  2. Create a new instance of your new resource.
  3. Set a property on it (eg myNewResource.tests = $scope.tests);
  4. Save it (myNewResource.$save())

http://jsfiddle.net/EjyW4/2/

It looks like what you were trying to do originally is better suited for $http (I put an example of that in the fiddle too).