发送邮件与restangular

时间:2016-05-06 20:01:44

标签: angularjs json rest restangular

我正试图通过邮寄一个带有restangular的json,但这不会给我任何回报。

代码:

$scope.userPost = {

        title: 'foo',
        body: 'bar',
        userId: 1
    }

    $scope.registerUser=function(){
        $scope.people = Restangular.all('post').post($scope.userPost);
        console.log($scope.people);
    }

它让我回答:

{ "restangularCollection": false, "$object": {} }
http://jsonplaceholder.typicode.com/post 404 (Not Found)

通常我将它与ajax一起使用,它会返回一个json:

$.ajax('http://jsonplaceholder.typicode.com/posts', {
  method: 'POST',
  data: {
    title: 'foo',
    body: 'bar',
    userId: 1
  }
}).then(function(data) {
  console.log(data);
});

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

Restangular适用于promises,所以,你应该做的事情如下:

$scope.registerUser = function () {
    Restangular.all('post').post($scope.userPost).then((data) => {
        $scope.people = data;
        console.log($scope.people);
    });
}

或者您可以利用其TypeNameHandling caution in Newtonsoft Json并执行以下操作:

$scope.registerUser = function () {
    $scope.people = Restangular.all('post').post($scope.userPost).$object;
}

答案 1 :(得分:0)

在Angular 4+中

/etc