2015-08-25 78 views
0

我想將JSON數據發送到後端數據庫。下面的代碼工作正常,直到我使用「$ window.location.href ='success.html';」將重定向添加到newPost函數中,添加重定向後,沒有任何內容發佈到數據庫。控制檯中也沒有顯示錯誤。我想,我應該檢查一下這個帖子是否成功,但我不確定如何正確地做到這一點。從服務器AngularJS添加重定向後無法將JSON數據發送到數據庫

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

回答

1
post.$save(); 
$window.location.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'; 
    } 

}); 

成功響應應該是:

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

我敢肯定,這就是正確的。試試看,讓我知道。

+0

謝謝!完美工作! – shredMER

相關問題