The integration of AngularJS with a redirect feature is causing issues with sending JSON data to the database

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>
}

Answer №1

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

must be:

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

I believe that should work correctly. Please test it out and inform me of the outcome.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discover how to achieve the detail page view in Vue Js by clicking on an input field

I'm a beginner with Vuejs and I'm trying to display the detail page view when I click on an input field. <div class="form-group row"> <label for="name" class="col-sm-2 col-form-label">Name</label> ...

What is the best way to divide an array of objects into three separate parts using JavaScript?

I am looking to arrange an array of objects in a specific order: The first set should include objects where the favorites array contains only one item. The second set should display objects where the favorites array is either undefined or empty. The third ...

Structure of JSON for a Collection of Items

I'm curious about the correct JSON structure for a list of objects. In our project, we are using JAXB to convert POJOs to JSON. Here are the options. Can you please advise on the right approach? foos: [ foo:{..}, foo:{..} ...

Building secure and responsive routes using next.js middleware

After setting up my routes.ts file to store protected routes, I encountered an issue with dynamic URLs not being properly secured. Even though regular routes like '/profile' were restricted for unauthenticated users, the dynamic routes remained a ...

PHP foreach loop encounters an unreachable if statement

I'm facing an issue with my code that reads from JSON and outputs each array. The problem arises when I use a foreach loop, where I encounter an unreachable if statement specifically for "type" being "rawbr". This issue persists e ...

How to Install MongoDB Using Command Line (Issue: WirdTiger feature not supported in this version of mongod)

I am encountering difficulties with the installation of Mongod on my system. Can someone please clarify what WirdTiger is and provide guidance on resolving this issue? I have already attempted to install MongoDB on my machine and created the Data\db f ...

Achieving JSON element sorting in the most effective way

https://i.stack.imgur.com/NQbdN.png Each array contains the following information: {{ id: 39, treaty_number: "qwe", insurant_name: "222", belonging_to_the_holding_company: "test", date_start: "2016-04-15", etc }} Is there a way to sort each array in asc ...

Django does not play well with JSON when using Ajax functionality

I am trying to implement an ajax request within a Django framework. However, I am encountering some difficulties when it comes to passing data from the client in json format. Everything works fine when I do not use Json. When I include dataType:'json& ...

Exploring the application of Nested Maps in extracting parameters for the getStaticPaths

Your data structure is organized like this: data = { "cse": { "first": [ "Computer Hardware Essentials", "Computer System Essentials", "Cultural Education" ], "second&qu ...

Is it possible to disable the "super must be called before accessing this keyword" rule in babelify?

My current setup involves using babelify 7.2.0 with Gulp, but I've encountered an error when working with the following code snippet: class One {} class Two extends One { constructor() { this.name = 'John'; } } The issue at hand i ...

IE page refresh causing jQuery blur to malfunction

Our website features a textbox with a watermark that appears and disappears based on focus and blur events in jQuery. However, we have encountered a unique issue with Internet Explorer browsers. Whenever a user clicks on the textbox, the watermark disapp ...

Adding to the Year Column in MySQL

I currently have the following MySQL format: {"2017": {"1": {"payed": 0, "charge": 0}}} Previously, I successfully used this format to execute SQL queries (such as reading/updating payed and charge values). However, I am facing an issue when trying to ad ...

AngularJS and ui-grid are a dynamic duo in web development

Could anyone provide guidance on removing the excess white space following rows in a grid? I am looking to have it perfectly align with the number of rows and remain sticky. No white space required in that area. ...

What is preventing me from accessing the props of my functional component in an event handler?

I've encountered a strange issue within one of my components where both props and local state seem to disappear in an event handler function. export default function KeyboardState({layout, children}) { // Setting up local component state const [c ...

Exploring the world of React-Bootstrap elements and properties

I'm currently diving into a Bootstrap home project and it's my first time working with Bootstrap. I came across a tag that has an 'inverse' attribute among others like 'fixedTop', 'fluid', and 'collapseOnSelect& ...

What causes the d3 force layout to fail and what steps can be taken to resolve it?

While experimenting with a force layout, I noticed that when I drag an item aggressively, it sometimes causes everything to freeze. This raises the following questions: What is the reason behind this issue? Is there any way to detect this and restart th ...

Guide on creating a sitemap using Express.js

I've been working with the sitemap.js package from https://www.npmjs.org/package/sitemap While I can add URLs to the sitemap manually, my challenge lies in adding URLs based on data retrieved from MongoDB. Since fetching data from MongoDB is asynchro ...

Getting the (x,y) Coordinate Value from jQuery Script and Saving it as a NSString

div tag is essential for applying bold, italic, and various other formatting options in UIWebview. My goal is to retrieve the position coordinates when a user interacts with the div tag using JavaScript/jQuery. I stumbled upon the required code on JSFiddl ...

Is it possible to use JQuery to target input nodes based on their values?

How can I check if any of the file input boxes in my list have a value using just one selector statement? Is it possible to achieve this with code like the following: $('input:file[value!=null]') Or is there another way to accomplish this? ...

Using a spray to parse a JSON array with nested JSON elements

I have a case class with string list fields and I am struggling to parse it from JSON. I have defined a JSON Reader : val jsonReader = new JsonReader[PeaceWatcherReport] { override def read(json: JsValue): PeaceWatcherReport = { val fields = js ...