AngularJS如何制作POST JSON

时间:2013-11-24 20:55:35

标签: javascript angularjs restangular

我正在尝试发帖但是下面似乎不起作用它没有发布,但console.log()看起来像一个看跌请求,即

http://127.0.0.1/api/v1/participant?account=%7B%22pk%22:1%7D&email=someemail@gmail.com

工厂

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

控制器

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

我做错了什么?

1 个答案:

答案 0 :(得分:2)

根据文档(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);

请注意帖子有一个输入

你可以在你的基地址添加'api / v1' - 不需要携带它(https://github.com/mgonto/restangular#setbaseurl

我还建议使用复数形式的资源路线,但这是一个有些人不遵循的惯例 - 我想这是一个品味问题