Using vanilla JavaScript with AJAX, the second asynchronous post will only be sent once the first post has been successfully sent

I am in the process of creating a HotSpot that offers internet access once users input their email addresses.

To make this function properly, I need to execute two separate AJAX posts:

  • The first one sends hidden username and password details to the router for granting internet access.
  • The second post sends the entered email address to the database, which requires internet connectivity.

A potential issue arises when setting async to false instead of true, as it triggers a warning message.*

xhr.open('POST', 'http://router/login', false);

Warning*: Synchronous XMLHttpRequest on the main thread has been deprecated due to its negative impact on user experience. For more information, visit .

In conclusion, I believe I should implement two asynchronous AJAX POST requests (to avoid warnings), with the second request (email) sent only after the completion of the first request (username and password).

Currently, I am facing an issue where sometimes the email gets added to the database successfully while other times it does not. Additionally, I do not want to incorporate a lengthy setTimeout delay for user redirection.

    <form accept-charset="utf-8" name="mail" onsubmit="return false;" method="post" id="mail">
        <h1>Hotspot</h1>
        <h2>To gain internet access, enter your email.</h2>
        <br />
        <input type="text" id="email" name="email" autofocus="autofocus" />
        <br />
        <input type="submit" value="Submit" id="submit_ok" name="submit_ok" /> <br />
    </form>

<script type="text/javascript">

document.getElementById("submit_ok").addEventListener("click", SendAjax);
    function SendAjax() {
        var email = document.getElementById("email").value;
        console.log(email);
        // Check if field is empty 
        if (email=="") {
            alert("Please enter your email.");
        }
        // Implement AJAX code to submit form
        else{
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://router/login', true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded", "Access-Control-Allow-Origin: *");
            xhr.send("popup=true&username=HSuser&password=SimpleUserPassword");
            {
                setTimeout(function(){
                    var xhr2= new XMLHttpRequest();
                    xhr2.open('POST', 'http://server/insertDB.php', true);
                    xhr2.setRequestHeader("Content-type", "application/x-www-form-urlencoded", "Access-Control-Allow-Origin: *");
                    var useremail = document.getElementById("email").value;
                    xhr2.send("Email="+encodeURIComponent(useremail));
                    setTimeout(function (){
                        location.href="http://server/redirected.html";}
                        ), 1000
                }, 2000);
            }
        }
    }

An edited snippet has been posted above to provide a possible solution. However, it is currently not functioning as expected. Posting this for further discussion regarding potential issues.

document.getElementById("submit_ok").addEventListener("click", SendAjax);
    function SendAjax() {
        var email = document.getElementById("email").value;
        console.log(email);
        // Check if fields are empty 
        if (email=="") {
            alert("Please enter your email.");
        }
        // AJAX code to submit form
        else{
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://router/login', true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded", "Access-Control-Allow-Origin: *");
            xhr.send("popup=true&username=HSuser&password=SimpleUserPassword");
            xhr.onreadystatechange = function () {
                var DONE = this.DONE || 4;
                if (this.readyState === DONE){
                    var xhr2= new XMLHttpRequest();
                    xhr2.open('POST', 'http://server/insertDB.php', true);
                    xhr2.setRequestHeader("Content-type", "application/x-www-form-urlencoded", "Access-Control-Allow-Origin: *");
                    var useremail = document.getElementById("email").value;
                    xhr2.send("Email="+encodeURIComponent(useremail));
                    xhr2.onreadystatechange = function () {
                        var DONEDONE = this.DONEDONE || 4;
                        if (this.readyState === DONEDONE){
                            location.href="http://server/redirected.html";
                        }
                    }
                }
            }
        }
    }

After testing this alternative, the results indicate that there are still shortcomings. While the computer eventually gains internet access (around 8 seconds), the "user" is redirected immediately, and the email is not consistently stored in the database even after making revisions.

I appreciate the assistance provided thus far. Thank you to everyone who has contributed.

Answer №1

If you're looking to handle XHR events, there are two approaches available. The first option involves defining a XHR event handler as shown below:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  var DONE = this.DONE || 4;
  if (this.readyState === DONE){
    ... make the second request (xhr2) here ...
    ... timeouts are no longer necessary ...
  }
}

As mentioned in https://en.wikipedia.org/wiki/XMLHttpRequest#The_onreadystatechange_event_listener, timeouts are obsolete when using proper event handling since it triggers when the request is fully completed.

The other simpler option is switching from async mode to sync by changing the parameter to false:

xhr.open('POST', 'http://router/login', false);

Answer №2

After the second ajax call is completed, adjust the document's location accordingly.

Make sure to monitor the readystate of your second ajax request as well. Once it reaches 'done', proceed with setting the document's location.

function SendAjax() {
    var email = document.getElementById("email").value;
    console.log(email);
    // Validate if fields are empty 
    if (email=="") {
        alert("Please enter your email.");
    }
    // Execute AJAX code to submit form
    else{
        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://router/login', true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded", "Access-Control-Allow-Origin: *");
        xhr.send("popup=true&username=HSuser&password=SimpleUserPassword");

        xhr.onreadystatechange = function () {
            if (xhr.readyState === XMLHttpRequest.DONE){
                var xhr2 = new XMLHttpRequest();
                xhr2.open('POST', 'http://server/insertDB.php', true);
                xhr2.setRequestHeader("Content-type", "application/x-www-form-urlencoded", "Access-Control-Allow-Origin: *");
                var useremail = document.getElementById("email").value;
                xhr2.send("Email="+encodeURIComponent(useremail));

                xhr2.onreadystatechange = function () {
                    if (xhr2.readyState === XMLHttpRequest.DONE) {
                        location.href = "http://server/redirected.html";
                    }
                };
            }
        }
    }
}

Answer №3

After hitting a roadblock with basic AJAX, I turned to using Async / Await and Promises to finally achieve success in resolving the issue.

Hopefully, this solution proves useful for someone facing a similar problem in the future.

Below is the code snippet that did the trick:

document.getElementById("submit_ok").addEventListener("click", sendAjax);

function resolveAfter05Second() {
  console.log("starting fast promise")
  return new Promise(resolve => {
    setTimeout(function() {
      resolve("fast")
      console.log("fast promise is done")
    }, 500)
  })
}

async function sendAjax() {
    let ax1 = await Ajax1 ("POST", "http://router/login")
    let fast = await resolveAfter05Second()
    let ax2 = await Ajax2 ("POST", "http://webserver/anti-xss.php")
}

function Ajax1 (method, url){
    return new Promise (function (resolve,  reject){
        let xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://router/login', true);
        xhr.onload = function(){
            if(this.status >= 200 && this.status < 300){
                resolve(xhr.response);
                console.log("Success!");
                console.log("You'r logged in.");
                console.log("XHR1 " + xhr.readyState);
                console.log("XHR1 " + xhr.status);
            }else{
                reject({
                    status: this.status,
                    statusText: xhr.statusText
                });
            }
        };
        xhr.onerror = function (){
            reject({
                status: this.status,
                statusText: xhr.statusText
            });
        };
        xhr.send("username=HSuser&password=SimpleUserPassword");
    });
}

function Ajax2 (method, url){
    return new Promise (function (resolve, reject){
        let xhr2 = new XMLHttpRequest();
        xhr2.open('POST', 'http://webserver/anti-xss.php', true);
        xhr2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr2.onload = function(){
            if(this.status >= 200 && this.status < 300){
                resolve(xhr2.response);
                console.log("Success!");
                console.log("You'r email is " + useremail + ".");
                console.log("XHR2 " + xhr2.readyState);
                console.log("XHR2 " + xhr2.status);
            }else{
                reject({
                    status: this.status,
                    statusText: xhr2.statusText
                });
            }
        };
        xhr2.onerror = function (){
            reject({
                status: this.status,
                statusText: this.statusText
            });
        };
        let useremail = document.getElementById("email").value;                 
        xhr2.send("Email="+encodeURIComponent(useremail));
    });
}

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

`Vue function is unable to find the definition of a variable`

I am struggling with utilizing a variable in a vue 3 application that has been emitted by a component called Mags. The variable is functioning properly in the importMags function, however, I am unable to access it within the handleSubmit function. It consi ...

React conditional statement within a map function embedded in a JSX element

Below is the function I have created to generate tabbed items: function ConstructTabs(props) { const tabs = props.tabs; const makeTabs = tabs.map((tab, index) => { <React.Fragment> <input className="input-tabs" id ...

The issue arises when attempting to reopen the jQuery UI dialog box after a div has been loaded

I am facing an issue with my survey results table where I have an EDIT button for each entry. Clicking on the EDIT button opens a dialog box with the survey results loaded from the database. After editing the answers, I want to save them in the database an ...

Compatibility issues between XMLHttpRequest and curl

Today, I am attempting to create a small XHR in JavaScript, Java, and C#, but it's not working for some reason... Below is the code snippet: var xhr = new XMLHttpRequest(); function init(){ xhr.open("POST","http://www.opsu.gob.ve/portal/controles/ ...

React hitting recursion limit due to excessive shouldComponentUpdate checks

I'm currently developing a real-time updating dashboard using React. The data for the dashboard components is fetched via an Ajax call and then passed to each component successfully. However, I encountered an issue with one specific component that re ...

Customizing Column Background Color with AngularJS

https://i.sstatic.net/OHFFF.png My current webapp highlights the day of the week on a table for the current day and the day two weeks from now. However, I'm facing an issue with adjusting the highlight if either of these days falls on a holiday. Curr ...

Creating a customized plugin in JavaScript that utilizes a local JSON/GeoJSON file

I am relatively new to Drupal and currently working on developing a custom map module. While my module functions correctly and displays the map along with relevant information, I encountered a challenge regarding calling my geojson file using a hard-coded ...

Tips for extracting Ajax responses that are delayed

I am attempting to retrieve the response code for an email form that I sent to a PHP mailer page using ajax. If the email is successfully sent, it will echo 1; otherwise, it will echo 0. Below is the code snippet of how I submit the form: $('form.aj ...

Implementing Decimal Input in Bootstrap-Vue for Google Chrome on Android

Issue is isolated to Google Chrome for Android (version 90.0.4430.210) and not present in other browsers. We have an input field that only allows numeric characters, structured like this: <b-form-input v-model="manualInput" class ...

Sorting `divs` based on the number of user clicks in JavaScript: A simple guide

I've implemented a script that tracks the number of clicks on each link and arranges them based on this count... Currently, everything is functioning correctly except when the <a> tags are nested inside a div. In such cases, the script ignores ...

Utilize a personalized useFetch hook in React.js to transmit a POST request and obtain a response

I recently came across a great resource on this website that provided the logic for a useFetch hook. My goal is simple - I want to send a post request and then map the response into a specific type. While this seems like it should be straightforward, I&apo ...

When using `console.log`, the object is displayed correctly. However, an error occurs when

Here is the code I've been working on: function parseJSONData(jsonData){ var property, type, name, identifier, comment, content; for(property in jsonData){ switch(property){ case "type": type = jsonData[ ...

The onClick event's detail property is persisting even after the React component has been replaced

In the onClick event, the value of event.detail indicates the number of clicks, with a double-click having event.detail = 2. It seems that when the React component being clicked is replaced, the event.detail value does not reset. This could be due to Reac ...

What is the best way to link assets within an Angular custom element (Web Components)?

After successfully creating a web component and referencing an image from my asset folder, everything was running smoothly on my local environment. However, when I published my custom element to Firebase hosting, I encountered some issues. When trying to ...

How to retrieve the width of an unspecified element using JavaScript

Seeking help with a div element that adjusts its size based on the elements it contains. How can I determine the final size of this dynamic div without checking the sizes of its internal elements? I've attempted to parse all the properties of the obj ...

Checking for Click Events in React Components

Recently, I created a React component named HelpButton with the following structure: import helpLogo from '../Resources/helplogo.svg'; function HelpButton(props) { const [isOpen, setisOpen] = React.useState(false) function toggle() { ...

Save user entries in a database array

I'm working on developing a platform for advertisements where users can create detailed profiles with images. To achieve this, I need to store the information in an array within a backend database for future retrieval. Below is an example of the backe ...

Detecting single letters in a sentence and changing their appearance using CSS

Looking to make a subtle change to text? I need to swap out single letters in a passage (I have a cat that ate a fish). Any ideas on how to do this? The goal is to input a block of text into a textbox, then display it in a div. I've had difficulty fi ...

Contrast between Q.defer() and the Promise() function

I am puzzled by the differing behavior of the following code when using Q.defer() as opposed to Promise() Scenario 1 : When Q.defer() is used getDocument(id) .then(function (response) { console.log('in first then') return 'from tw ...

If the width of the table is set to 100% in the CSS, the legend in the Flot chart will automatically shift to the

When the CSS for the table is set to { width:100%}, the Flot chart's legend moves to the left side. Is there any way to maintain the table { width:100%} while also preventing this shift, considering that the CSS is applied site-wide? Here is a jsfid ...