Prometheus
Prometheus

Reputation: 33625

AngularJS how to make a POST JSON

I'm trying to make a post but below does not appear to work it does not post but console.log() looks like a put request i.e

http://127.0.0.1/api/v1/participant?account=%7B%22pk%22:1%7D&[email protected]

factory

app.factory('CbgenRestangular', function(Restangular) {
    return Restangular.withConfig(function(RestangularConfigurer) {
        RestangularConfigurer.setBaseUrl('http://127.0.0.1');
    });
});

controller

$scope.participant = {email :$scope.email, password: "", account:{pk:1}};
CbgenRestangular.all('api/v1/participant').post("participant", $scope.participant);

What I'm I doing wrong?

Upvotes: 0

Views: 105

Answers (1)

surui
surui

Reputation: 1542

according to the documentation (https://github.com/mgonto/restangular#lets-code):

// First way of creating a Restangular object. Just saying the base URL 
var baseAccounts = Restangular.all('accounts');

var newAccount = {name: "Gonto's account"};

// POST /accounts 
baseAccounts.post(newAccount);

note that post has a single input

you can add 'api/v1' to your base address - there's no need to carry it around (https://github.com/mgonto/restangular#setbaseurl)

I would also suggest using the plural form for a resource route, but that's a convention that some people don't follow - I guess it is a matter of taste

Upvotes: 2

Related Questions