Search code examples
javascriptangularjsngresource

Angular ng-resource POST sending query params not JSON


I'm trying to get my first ng-resource example working. The problem is it's sending the data as query params rather than JSON.

I have this service for my REST resource. REST_URL_PREFIX is just a constant.

angular.module('myApp')
  .service('Test', ['$resource', 'REST_URL_PREFIX', function Test($resource, URL_PREFIX) {
     return $resource(URL_PREFIX + 'test/:id');
  }]);

Get's used like this.

var obj = {name: 'Test', num: 12345};
var objRest = new Test();
objRest.$save(obj);

I can see in the console that this calls the web service like this:

POST http://server/rest/test?name=Test&num=12345 

What I'd like is for it to call like this:

POST http://server/rest/test  

But with the JSON in the body of the request.

{name: 'Test', num: 12345}

The content type of the request is application/json automatically so I would've thought it would send JSON?

Have I missed something? I did spend quite some time searching and there are a number of similar questions but none solved my problem.

Edit
Ultimately I want this to submit details from a form.

$scope.submit = function(test) {
  > Code to save test parameter using Test resource required here
}

Thanks


Solution

  • $scope.createTest = function () {
       var newTest = new Test;
       newTest.name = "new name";
       newTest.nume="123";
    
        newTest.$save();
    
    };