Executing $http.get following $http.post in AngularJS

After my $http.post method has completed, I want to initiate my $http.get method. To achieve this, I have created a controller that triggers upon clicking a button.

var app = angular.module("loginApp", []);

app.controller("ctrlLogin", function($scope, $http, $window, $timeout){
$scope.name = "";
$scope.key = "";

var message = {name: $scope.name, key: $scope.key};

$scope.setData = function(){
    message.name = $scope.name;
    message.key = $scope.key;

    $http.post('/getData', message)
      .then(function() {
          console.log("msg sent");

      }, function error() {
        console.log("msg failed");

      }).then(
            $http.get('/doLogon')
            .then(function() {
                console.log("logon");
            }, function error() {
                console.log("doLogon failed");

            }));
};
});

Currently, "logon" is displayed before "msg sent". I require both functions to be executed simultaneously when the same button is clicked.

Answer №1

I am unsure of the reason for having two then statements in your code snippet. It might be worth reviewing that part:

$http.post('/getData', message)
  .then(function() {
      $http.get('/doLogon')
        .then(function() {
            console.log("logon");
        }, function() {
            console.log("doLogon failed");
        })
  }, function() {
    console.log("msg failed");
  })

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

Stop the scrolling behavior from passing from one element to the window

I am facing an issue with a modal box window that contains an iframe. Inside the iframe, there is a scrollable div element. Whenever I try to scroll the inner div of the iframe and it reaches either the top or bottom limit, the browser window itself start ...

Ajax request in Rails not receiving a response from the controller

After troubleshooting a simple GET request to the controller action, I confirmed that the request is being made successfully and there are no issues with the controller executing the action. However, I am not receiving any response data. $.ajax({ url: ...

AngularJS - Multi-controller Data Calculation

I am currently in the process of developing an Angularjs application. The project is quite extensive and requires segmentation into multiple Controllers to manage effectively. One challenge I am facing is performing calculations across these controllers. ...

Optimizing Div Heights with jQuery Scroll

As I scroll down, I want the height of a specific div to decrease instead of the window itself scrolling. The code snippet below illustrates the simple div header setup that I am using: <html> <head> <title>Test</title> ...

Await the sorting of intervals

Looking for a solution to re-execute a hand-made for loop with a delay of 10 seconds after it finishes indefinitely. I've attempted some code, but it keeps re-executing every 10 seconds rather than waiting for the loop to finish before starting again. ...

What purpose does AJAX / AJAJ serve in Sails Express JS apart from facilitating asynchronous processing?

When considering practicality, what are the strengths and weaknesses of using AJAX or AJAJ in comparison to other standards like HTML post followed by: res.render('returnView', {outputVariable: V }) Does it matter if processing the input takes ...

What could be causing my webpage to freeze every time a filter button is selected?

Tasked with developing a webpage similar to Pinterest by utilizing data from a JSON response. Each JSON object contains a service_name key, which can be manual, twitter, or instagram. I made an effort to implement three filter buttons to only display the r ...

Exploring different web pages with AngularJS on localhost in ASP.NET

Exploring angularJS has been a challenging yet rewarding experience for me, especially when dealing with routing. Currently, I am working on an ASP .NET project using Visual Studio. The project is being run locally on Google Chrome. This is the structure ...

Exploring the Positives and Negatives of Using JQuery and Glow JavaScript Libraries

Is there a detailed analysis available that compares JQuery with the BBC's Glow JavaScript libraries? ...

Dealing with JS and Spring issues

I created a simple script that retrieves longitude and latitude values and populates an input form. Here is the HTML: <p id="demo">Click the button to get your coordinates:</p> <button onclick="getLocation()">Try It</button> <sc ...

Transform the characters within a string into corresponding numerical values, calculate the total sum, and finally display both the sum and the original string

I'm looking to convert a string containing a name into numerical values for each character, ultimately finding the sum of all characters' numerical values. Currently, only the first character's value is being summed using .charAt(). To achie ...

Dealing with undefined or null values when using ReactJS with Formik

Issue Resolved: It seems that Formik requires InitialValues to be passed even if they are not necessary. I'm currently working on a formik form in React, but every time I click the submit button, I encounter an error message stating "TypeError: Canno ...

Update D3 data, calculate the quantity of rows in an HTML table, and add animations to SVGs in the final

Attempting to update data in an HTML table using D3 has proven to be quite challenging for me. My task involves changing the data in multiple columns, adjusting the number of rows, and animating SVG elements in each row based on new data arrays. Despite tr ...

Animate the tubular geometric pathway by continuously updating its values

When creating a TubeGeometry, I supplied a SplineCurve3 / CatmullRomCurve3 path as a parameter. My goal is to update the position of each point on the path using geometry.parameters.path.points[1].y += 0.01; within the requestAnimationFrame loop. Even tho ...

Developing error/result functions in NodeJS

I'm struggling to comprehend the process of creating functions that return in the format of (err, result) for an Express app. The current structure of my db query function is as follows: pool.query( 'SELECT id FROM users WHERE email = ? LIMIT ...

Resolving unexpected behavior with res.locals and pug integration

I have a function in my app.js that allows the user-id to be accessible in pug templates. app.use(function (req, res, next) { res.locals.currentUser = req.session.userId; next(); }); When logged in, I can access the id. However, when not logged in, t ...

The dropdown menu adjusts its value based on the selected radio button

When I select the "12 hour" radio button, the drop-down values change to 1 am - 12 am and 1 pm - 12 pm. If I select 24 hours, then the values change to 1-24. Below is my code: <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script> ...

Issue with Jquery .scroll() not functioning in Internet Explorer when using both $(window) and $(document). Possible problem with window.pageYOffset?

Here is the code snippet I am currently struggling with: $(window).scroll(function() { var y_scroll_pos = window.pageYOffset; var scroll_pos_test = 200; if(y_scroll_pos > scroll_pos_test) { $('.extratext').slideDown(&a ...

Create an Angular service that outputs class properties as observables

I am trying to accomplish the following task: I have a component with a UserService in it. After a user logs in, I want to call this service to retrieve user data from the backend and store it in a service field. Then, when the main page is reloaded, I wan ...

Tips for initiating a scrollable (overflow-y: scroll) div in the middle rather than the top while scrolling

I am working on a code where I have a scrollable div with overflow-y: scroll. However, the default scrolling starts at the top of my div when I want it to start in the middle. Can anyone suggest a solution for this issue? export function Portfolio({ chi ...