Issues with XMLHttpRequest functionality

I'm currently working on a project where I need to update the TextArea element in my HTML every 2 seconds dynamically, without reloading the page. The issue I'm facing is that when I use alert(request.readystate), it returns undefined instead of a number between 1 and 4. However, when I tried

if(request) alert("Request object")
, it correctly alerts "Request object". I've been stuck on this problem for hours and can't seem to figure out why it's not working!

Here's my code:

<script type="text/javascript">

    function init(){
      var url="http://www.suchandsuch.net/ChatBowl/text.txt";
      var request= new XMLHttpRequest();
      request.open("POST",url,true);
      alert(request.readystate);
      request.onload= function(){
        if (request.readystate ==4 && request.status == 200){
          document.getElementById('textarea').innerHTML=request.responseText;
        }  
      }
        request.send(null);

     }


    var int=self. setInterval('init()', 2000);

  </script> 

I would greatly appreciate any help.

Answer №1

Remember, JavaScript is case sensitive and you must be careful with your spelling:

readyState not readystate

Check out the MSDN docs for more information.

Also, make sure to assign the callback function to onreadystatechange:

var url="http://www.suchandsuch.net/ChatBowl/text.txt";
var request= new XMLHttpRequest();
request.onreadystatechange = function(){
    alert(request.readyState + " " + request.status);
    if (request.readyState ==4 && request.status == 200)
          document.getElementById('textarea').innerHTML=request.responseText;
    };

request.open("POST", url, true);
alert(request.readyState);  

Don't forget to include request.send(); at the end.

Answer №2

perhaps you could consider

request.onload= function(){

experiment with

request.onreadystatechange = function(){

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

What is the best way to activate only one dropdown menu from a collection of others produced within a .map

I feel a bit lost with this issue, but I'm hoping for some guidance! Within my MenuWithDropdown Component, I've set up several dropdowns. Each dropdown is supposed to be activated with the handleClick() function. Normally, I would use this.state ...

No styles are appearing on a specific element after running a specific jQuery function on that element within a Vue page

I recently integrated JQuery-AsRange (https://github.com/thecreation/jquery-asRange) into my vue.js project. Everything functions as expected within the .vue page, however, I am facing an issue with css styling not being applied. The css styles should be ...

What's the best way to set up server-side pagination for mui-datatable?

Is server-side pagination for mui-datatable implementation a complex task? Challenges: I am facing difficulties in capturing the user-selected value from the rowsPerPage options. When a user selects '15', how can I update these values within ...

Having trouble with running the command "npx create-nuxt-app <project-name>"?

When attempting to create a Nuxt.Js app using npx, I encountered the error shown in the image below. https://i.sstatic.net/fnQT6.png ...

jspdf generates blank PDF files

I am trying to use JsPDF to generate a PDF from content within a Section tag. I have followed various guides but none of them seem to be working for me. Since there is no demo code available, I am turning to this platform in hopes of finding a solution. A ...

Struggling with implementing Bootstrap modal JavaScript in Rails 4, nothing seems to work!

This situation has been addressed in numerous posts, but despite my efforts to find a solution among them, I have yet to come across one that works for me. I am faced with the task of opening a modal that requires JavaScript modifications before being dis ...

"To toggle between VueJS input checkbox and class change, users may need to click the element

I decided to create a list of items with checkboxes. The goal was to tick the checkbox and add a class to the div upon clicking it. Unfortunately, when I click on the checkbox, it only ticks the checkbox itself. I actually need to click it again for the c ...

Getting parameter names (or retrieving arguments as an object) within a method decorator in TypeScript: What you need to know

I am currently working on creating a method decorator that logs the method name, its arguments, and result after execution. However, I want to implement a filter that allows me to choose which parameters are logged. Since the number and names of parameter ...

Issues with compilation arise in Angular5 application due to CSS import errors stemming from a corrupt library

Currently working on an application using the latest Angular version. I recently imported a library for a component, but it has caused more issues than assistance. Despite that, I am still encountering compilation errors and reluctant to start over with a ...

Change the width of the webpage inside the iframe

I am working with an iframe that has a fixed width and height of 400px. I want to load a web page in the iframe, but I need to adjust the width of the source to fit my iframe perfectly. The height is not an issue as I can add a scrollbar if needed. Here ...

Switch to a different form when clicked using JavaScript

Can you assist me with my code issue? The scenario is as follows: I have two forms - one for registration and one for login. I want to dynamically replace the login form with the register form when the user clicks on the "Sign up" link. Similarly, if the ...

Problem with translating a variable into a selector in JQuery

When attempting to make my Jquery code more flexible, I decided to extract the selector and access it through a variable. However, despite creating variables for both selectors, neither of them seem to be functioning properly. I am confident that the issue ...

developing a shader that transitions between day and night based on the movement of a light source

I've set up a scene with a sphere illuminated by a DirectionalLight to simulate the sun shining on Earth. My goal is to incorporate a shader that displays the earth at night on the unlit portions of the globe and during the day on the lit areas. Event ...

A step-by-step guide on setting up an event listener for dynamically generated HTML using JavaScript objects

In my JavaScript code, I am creating object instances that include HTML elements in the form of buttons. These buttons are dynamic and have words generated dynamically as well. I want these buttons to execute certain functions when clicked. The challenge I ...

Having difficulty deleting an entry from a flatList in a React Native component when using the filter method

I'm currently facing an issue with deleting an item from my flatlist in React Native. I've been attempting to use the filter method to exclude the list item with the ID entered by the user for deletion, but it's not working as expected. I&ap ...

Creating a PHP form with multiple buttons

I'm facing a problem with my form that contains 2 buttons. Depending on which button is selected, the data in the database should be either deleted or edited. I have separate pages for editing and deleting using SQL statements (questionedit and questi ...

Sorting through the Axios response

I'm currently implementing a filter on an axios response. It seems like the filter is functioning correctly, but my components are not receiving the expected data, only the correct number of records. methods: { loadItems() { // Initialize ...

Gracefully Switching Between Various Functions

Suppose I have a collection of functions that perform various tasks: function doSomething() { console.log('doing something'); } function accomplishTasks() { console.log('accomplishing tasks'); } function executeAction() { console. ...

Sending a JSON object as a prop from one React.js component to another

I've been attempting to pass a JSON object retrieved by calling a REST API after submitting a form with user input. While I am successful in transferring the data to another component within the same file (this component is responsible for iterating o ...

What steps should be taken to configure Multer so that it delays saving an image until after a successful database entry using Mongoose has been made?

In the typical Multer setup, files are saved immediately, as illustrated in the following basic example: const multer = require('multer'); const upload = multer({ dest: 'uploads/' }); const app = express(); app.post('/profile&apo ...