添加了重定向后,AngularJS无法向DB发送JSON数据

AngularJS Unable to send JSON data to DB after adding redirect

本文关键字:DB 发送 JSON 数据 重定向 AngularJS 添加      更新时间:2023-09-26

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

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>
}
post.$save();
$window.location.href = 'success.html';
应:

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

我很确定那是对的。试一试,让我知道。