Once I have verified the user's credentials through a POST request, I will proceed to make a GET request

I am in the process of constructing a dashboard that automates logging into an API and updating specific data elements. I have successfully managed to login and authenticate, but I am unsure how to proceed with chaining the GET request after the POST action.

I've attempted to learn from various YouTube tutorials, creating functions and associating them with buttons and divs, but unfortunately, I am unable to display the retrieved data. The initial code segment executes the login process smoothly, but then it gets stuck and times out. I even tried making the login synchronous by calling a second open function, but it was unsuccessful.

<script type="text/javascript">
    const xhr = new XMLHttpRequest();
    var data = 'username=user&password=password';

    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == "4")
    {
            if (xhr.status == "200")
            {
                console.log(xhr.responseText);
            }

            if (xhr.status = "404")
            {
            console.log("FnF");
            }

        }
    }
    xhr.open('post','https://apiServer:8443/api/login', true)
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
    xhr.setRequestHeader("Accept", "application/xml")
    //xhr.open('get', 'https://apiServer:8443/api/resource/items', true);
    xhr.send();

I anticipate the login process to be executed discreetly in the background, while the GET request fetches the data to be displayed in a div element (I will refine the XML response once I have resolved the data retrieval issue).

Answer №1

Your data variable in the code is never utilized:

const xhr = new XMLHttpRequest();
var data = 'username=user&password=password';

xhr.onreadystatechange = function()
{
    if (xhr.readyState == "4")
    {
        if (xhr.status == "200")
        {
            console.log(xhr.responseText);
        }

        if (xhr.status = "404")
        {
        console.log("FnF");
        }

    }
}
xhr.open('post','https://apiServer:8443/api/login', true)
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xhr.setRequestHeader("Accept", "application/xml")
//xhr.open('get', 'https://apiServer:8443/api/resource/items', true);
xhr.send(data); // <====== HERE

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

Unable to display shadows in Three.js

I've hit a roadblock with the shadow effect at the moment, despite trying various solutions for hours, it's still not appearing. Can anyone point out what I may have done incorrectly? Below is the code snippet for my current scene: //Setting up ...

Issues with ng-show functionality occurring during the initialization of the webpage

While working on a webpage using HTML, CSS, and Angular.js, I encountered an issue where the page content would not display properly upon loading. The objective was to show selected content based on user choices from a dropdown menu. Although the filtering ...

Is it possible for me to overlap a text over hidden text, and if the hidden text becomes visible through JavaScript, my text will shift to the right of the now visible hidden text?

I'm currently working on a website for a company that requires users to complete all the information before proceeding. To achieve this, I created a form with the following code: <form action="Owners Infoback.php" onsubmit="return validateFo ...

Upon registration, the user's information is successfully stored in the mongoDB database, even if the selected "username" is already in

I am currently working on a web application using the MERN stack. When registering a user with an existing email either through Postman or the front-end form, the user is not added to the database. The same logic is applied to check if the chosen username ...

Simple guide on how to use AJAX (without jQuery) and PHP to count the number of records

As a novice programmer, I am attempting to tally the number of records in a table. Despite perusing various code snippets, I am unable to seamlessly integrate them to pass the PHP result to my javascript code. Here is the current state of my code: showsca ...

What is the best method to make an email address distinct?

<body> <p>Please input your email address:</p> <input id="email" style="margin-bottom: 20px; margin-top: 2px;" type="email" placeholder="Email Address"> <input onclick= "validateEmail(email)" type="su ...

Issues encountered while sending HTML Form data to MySQL with Node JS

I have been experimenting with a basic html form to mysql using nodejs, but unfortunately it is not functioning as expected. The HTML file is named index.html and the Node.js file is called test.js. Below you can find my code: My HTML <!DOCTYPE html&g ...

Clicking on a href link inside a scrollable div-container causes it to malfunction, causing the page to jump

I have a draggable div container that contains dynamically generated content. In order to display this container, I use the following code: $( function() { $( "#dialog-message" ).dialog({ modal: true, height: 400, buttons: { Fertig: functi ...

The Electronjs application encountered an error while loading with the message "ReferenceError: require is not defined."

Having trouble with an electron application as I encounter an error when running npm start: $ npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="483c2d3b3c087966786678">[email protected]</a> start C:& ...

What are the potential reasons for a function in React not being identified as a function?

I recently started learning React and decided to follow this YouTube tutorial on creating a TO DO LIST using React. https://www.youtube.com/watch?v=E1E08i2UJGI Everything seems to be in place, but when I try to interact with my form by typing something an ...

Adjust the width of every column across numerous instances of ag-grid on a single webpage

I am facing an issue with Ag-grid tables on my webpage. I have multiple instances of Ag-grid table in a single page, but when I resize the browser window, the columns do not automatically adjust to the width of the table. We are using only one Ag-grid in ...

Isolating JavaScript Ajax codes in a Laravel 8 js file results in functionality issues

Having recently delved into the world of Ajax, I've encountered some issues. Allow me to do my best in explaining the problem at hand. Currently, I'm engaged in a Laravel 8 project. In this project, users are given the choice to select an image, ...

Quasar Troubles with Touch Swipe Gestures

I'm facing two issues, the first being that the directives are not functioning as expected. For example, I've implemented a swipe only to the right: <div class="q-pa-md row justify-center"> <q-card v-touch-swipe. ...

The method defined in user.model.js cannot be utilized in mongoose and node

When developing an app with node and mongoose, I encountered a peculiar issue while testing. Below is my auth.index.js file for user login. auth.index.js: var express = require('express'); var mongoose = require('mongoose'); var passp ...

AJAX enabled DB row updates

I'm currently working on troubleshooting an issue with updating a row using AJAX. When the submit button is clicked, I want the value of "poke" to increase by +1 and also update in the database. The goal is to make everything work seamlessly with AJAX ...

Utilizing Jquery's Selectable IDs in a repetitive manner

I'm a newbie to jQuery and I'm attempting to create two lists where each item can be selected. Check out my code: <!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI s ...

Exploring the use of properties in JavaScript

I recently began learning Vue.js 2, but I encountered an issue when passing props to a child component. Here's the code snippet where I pass the prop: <div class="user"> <h3>{{ user.name }}</h3> <depenses :user-id="user.id"&g ...

Expanding a feature that modifies the CSS properties of every input element

Creating a text tracking (letter-spacing) demonstration where range sliders are used to update CSS properties. I'm looking to improve the functionality so that the labels also update dynamically, without repeating output2.textContent = this.value;. An ...

What is the best way to retrieve the browser language using node.js (specifically express.js)?

element, consider the scenario where a user requests a specific page and you are interested in determining the language set in their browser on the server side. This information is crucial as it enables you to customize the template with appropriate messa ...

We encountered a TypeLoad Exception while working with Microsoft Dynamics CRM, resulting in an unexpected error

An error is occurring in the ajax call's (POST) error handler on our CRM 2013 Online production instance. The XHR.responseText message displays the following: "error": { "code": "-2147220970", "message": { "lang": "en-US", "value": "System.TypeLoadEx ...