The AJAX request is coming back with a readyState value of 1

Currently, I am working with Flask using Python. My goal is to return HTML table rows in an AJAX call from the Flask method.

The HTML page contains:

<div>
<table id="tableQuestions">

</table>
//AJAX Requests to get
function loadQuestions() {

  var xmlHttp = new XMLHttpRequest();

   xmlHttp.onreadystatechange = function() {
   alert(xmlHttp.readyState + " " + xmlHttp.status);
   if (xmlHttp.readyState==4 && xmlHttp.status == 200) {
       var response = xmlHttp.responseText;
       console.log(response);
       document.querySelector("#tableQuestions").innerHTML=response;
   }
   xmlHttp.open("GET", '/gS', true);
   xmlHttp.send(null);
  }
}

The Flask route is as follows:

#test.mix() returns html table rows

@gbp.route('/gS')
def gS():
    test=mn()
    response = make_response(test.mix(quesCount=4),200)
    response.mimetype="text/html"

While inspecting in Safari debugger, I noticed that although the responseText displays the table data from the server, the readystate remains at 1 and status is 0.

https://i.sstatic.net/Azm1s.png

Any suggestions on how I can resolve this issue would be greatly appreciated.

Answer №1

You currently have

xmlHttp.open("GET", '/gS', true);
xmlHttp.send(null);

implemented in your xmlHttp.onreadystatechange callback function, causing unexpected behavior.

Consider utilizing the following code snippet (not tested) for improved functionality:

//Function to handle AJAX Requests
function loadQuestions() {
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET", '/gS', true);

  xmlHttp.onreadystatechange = function() {
    alert(xmlHttp.readyState + " " + xmlHttp.status);
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
      var response = xmlHttp.responseText;
      console.log(response);
      document.querySelector("#tableQuestions").innerHTML = response;
    }
  }

  xmlHttp.send(null);
}

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

JavaScript Object Instantiation within the Object Prototype

If we consider a scenario where there are 3 chapters of a novel each with its own unique URL structure: Chapter 1 = /1.html Chapter 2 = /2.html Chapter 3 = /3.html Now, to implement object-oriented principles using jQuery, the idea is to create two JS ...

The tension settings in Chart.JS appear unusual

I recently updated to ChartJS v4.0.1 and noticed a new option called tension for curving the line chart. However, I'm not satisfied with how it looks. The tension option ranges from 0 to 1, and I've experimented with different values to enhance ...

React - Image Uploader exclusively accepts images with transparent backgrounds

I need to verify if an image has a transparent background and reject it if it does, but accept it if it doesn't. However, I am facing an issue where the hasAlpha function is not triggering an 'error' alert when the image contains a backgroun ...

Jquery plugin experiencing a malfunction

I am encountering an issue with my custom plugin as I am relatively new to this. My goal is to modify the properties of div elements on a webpage. Here is the JavaScript code I am using: (function($) { $.fn.changeDiv = function( options ) { var sett ...

Tips for updating data within an array using a copied object

Just starting out with JavaScript Could you assist me with a basic inquiry, please? For instance, I have an array like this: const array = [{ id: 1, name: 'Ferrari', type: 'F40', price: '350 000$', countr ...

How can I effectively remove event listeners while still preserving the context in a stimulus controller that listens to events multiple times?

My HTML page has the following controller setup: ... <div data-controller="parent"> <div data-target="parent.myDiv"> <div data-controller="child"> <span data-target="child.mySp ...

Retrieve information from a database in JSON format using AngularJS, then showcase it within the textbox

As a beginner in AngularJS, I am trying to retrieve data from my database and display it in a textbox. This is my code snippet: $scope.getData = function(){ $http.post('query?action=get',{ }) .then(function(response){ $sc ...

Display nested array objects of varying levels within VUE

How do I display nested elements on the UI when dynamically generated based on a parent element's selected option (e.g. List)? For example, in the code below, when creating a field and selecting the List option, another nested should appear, and so o ...

Issue: Why does the error "this.geometry.morphTargets is not defined" occur exclusively when using the UTF8Loader?

I encountered Error: this.geometry.morphTargets is undefined at line 14558 while using this UTF8Loader code: var loader = new THREE.UTF8Loader(); loader.load("MD/ben-utf8.js", function (geometry) { callbackModel(geometry, 400, 0xffffff, 0, ...

Unable to insert the model into the MongoDB database

I am a beginner when it comes to creating APIs. I am currently attempting to add an entry to a MongoDB database. I have successfully established the database connections. Below is the code I have written for inputting data into the MongoDB. // Node Module ...

Establish the condition if the array is present

I am looking to conditionally set a state based on the existence of data retrieved from the server. const [names, setNames] = useState([...details?.names]) The details object is passed in as props and fetched from the database. The array details.names com ...

Sending a parameter to a different function (on a separate webpage)

At the start of my webpage, there are two radio buttons on the first page, each with its own value. Upon clicking a link to move to the second page, a for loop is activated to grab the value of the selected button. The script has been tested and works as e ...

WordPress data structure containing Ajax PHP Class action for the myfunction in ds_calculators

It's been quite some time since I last delved into AJAX and Wordpress development. In the past, my approach was pretty straightforward - I would define my function within functions.php and then add actions using the following code: add_action('wp ...

Increment Counter on Presence of Items in a Table with jQuery

Does anyone know how to properly increment a counter in jQuery based on certain words found within a table? I am currently encountering an issue where my counter is only increasing by 1 instead of the expected 2. Here is the HTML and jQuery code snippet: ...

Executing the Npm audit fix --force command will automatically implement its own suggestions

Despite coming across countless similar questions, none of them have been helpful in addressing my issue. I am working on resolving critical vulnerabilities. I have executed npm update, npm audit fix, and npm audit fix --force multiple times, but the prob ...

PHP - API does not execute in Ajax request without using a delay or print_r function

I am having an issue with a script that runs using jQuery via onClick event. I pass some information through that function. Below is my Event script. $.post("page.php", { ... }, function(ret){ alert(ret); }); Within page.php, I am calling an API to send ...

Streamlined Variable Passing

Currently, I am immersed in a project that requires passing variables between functions, but unfortunately this is an area where my Python skills are lacking. I only recently started learning Python about a month ago. I'm hoping to find the most strai ...

Tips for configuring a channel object with the channel id in discord.py?

Hey everyone, I'm working on creating a function that can determine if certain channels in my discord server are full, but I'm struggling to specify them. I thought about using an idea, but I couldn't figure out how to set it to a channel ob ...

Discover the magic of TransformToggle and slideToggle in Javascript, HTML, and CSS

Working on a website coding project and ran into an issue. Trying to utilize an img as a link to slideToggle content. It's working, but I'd like the img to rotate 90deg on the first click and -90deg on the second one. Any solutions are greatly ap ...

What could be causing the sporadic functionality of my jQuery image resizing code?

Seeking help for an issue I am facing with my jQuery code. I have been trying to scale a group of images proportionally within an image carousel using jCarousel Lite plugin. However, the resizing code seems to work randomly and I can't figure out why. ...