I have encountered an issue when trying to send JSON data to a backend database. Everything works perfectly until I introduce a redirect within the newPost function using "$window.location.href = 'success.html';" Once the redirect is added, no data is posted to the database and there are no error messages shown in the console. I believe I need to verify if the post was successful, but I am unsure about the correct approach to do so.
app.controller('FormCtrl', function($scope, $filter, $window, getData, Post, randomString) {
// Retrieving 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';
}
});
Response received from Server successfully
RETURN CODE: 200
RETURN HEADERS:
Content-Type: application/json
RETURN BODY:
{
"ref":<string>,
"uuid":<string>
}