AngularJS添加重定向后无法将JSON数据发送到DB

时间:2015-08-25 20:57:00

标签: javascript json angularjs database ionic

我正在尝试将JSON数据发送到后端数据库。以下代码正常工作,直到我使用“$ window.location.href ='success.html';”将重定向添加到newPost函数中。添加重定向后,没有任何内容发布到数据库。控制台中也没有显示错误。我想,我应该检查帖子是否成功,但我不确定如何正确地做到这一点。

app.controller('FormCtrl', function($scope, $filter, $window, getData, Post, randomString) {
   // Get all posts
   $scope.posts = Post.query();

  // Form data for creating a new post with ng-model
  $scope.postData = {};
    $scope.$on('updateImage', function () {
        $scope.postData.attachment = getData.image;
    });
    $scope.postData.userid = "Mango Farmer";
    $scope.postData.uuid = randomString(32);
    $scope.$on('updateGPS', function () {
        $scope.postData.gps = getData.gps;
    });
    $scope.postData.devicedate = $filter('date')(new Date(),'yyyy-MM-dd HH:mm:ss');

  $scope.newPost = function() {
    var post = new Post($scope.postData);
    console.log(post);
    post.$save();
    $window.location.href = 'success.html';
  }

});

服务器的成功响应

RETURN CODE: 200
RETURN HEADERS:
Content-Type: application/json
RETURN BODY:
{
"ref":<string>,
"uuid":<string>
}

1 个答案:

答案 0 :(得分:1)

post.$save();
$window.location.href = 'success.html';

应该是:

post.$save().then(function(response) {
    $window.location.href = 'success.html';
});

我很确定这是正确的。试一试,让我知道。