What may be causing my Ajax call to get stuck at readystate 1 and not progress further

I've double-checked all my code, yet I'm not getting any response from my Ajax call. Can someone help me figure out what's wrong?

function fetchPokemonData() {

const apiRequest = new XMLHttpRequest();

apiRequest.open('GET', 'https://pokeapi.co/api/v2/pokemon', true);
console.log(apiRequest.readyState);

apiRequest.onload = function() {
    if(apiRequest.status === 200 ) {
        console.log(true)
        let data = JSON.parse(apiRequest.responseText);
        console.log(data)
        for(let item in data) {
            console.log(data[item])
        }
    }
}
console.log(apiRequest.readyState)
apiRequest.send()
}

The readystate remains stuck at 1 and I can't seem to find the issue.

Answer №1

xhr.readyState may not provide the solution you are looking for with the code as it is written; it would be helpful to troubleshoot whether onload is being executed.

console.log(xhr.readyState); // synchronous
xhr.onload = function() {
   // asynchronous code inside the function call, triggered by onload event
}; 
console.log(xhr.readyState) // synchronous, does not wait for AJAX response

If you place console.log(xhr.readyState) within your callback function, it will display different values, but don't forget to handle onerror as well...

I recommend utilizing the addEventListener listeners API instead of traditional event handlers.

Hopefully this information proves useful!

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

The output of new Date() varies between app.js and ejs

app.get("/test",function(req,res){ var d = new Date(); res.send(d); }); When I access mydomain/test, it displays the output "2019-03-19T04:50:47.710Z" which is in UTC. app.get("/testejs",function(req,res){ res.render("testejs");}); Below is the content ...

Suggestions for returning success from server side to ajax call in Ember.js

I am new to Ember and jQuery, starting out as a beginner. I'm working on creating a login page but I'm unsure about the server-side response for failed logins. My current code looks like this: App.LoginController = Ember.Controller.extend({ ...

Having trouble with installing Recharts through npm

When I try to install recharts using npm, I encounter the following error message in my console: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! ...

Ensuring Angular2 Javascript is validating only numerical input and not accepting characters

Access the full project here (excluding nodes_modules): Note: After running the project, all actions related to this issue can be found in the "Edit All" section of the website. Click on that to view the table. The main objective of this website section ...

"Disabling a FormControl within a FormArray in Angular 4: A Step-by-

I've come up with a code snippet below that I thought would disable the FormControl in a FormArray. some.component.html <form [formGroup]="testForm"> <div *ngFor="let num of countArr"> <input type="text" formNameArray="arr ...

Guide on initializing a Redux toolkit state with an array of objects or local storage using TypeScript

Currently, I am attempting to set an initial state (items) to an array of objects or retrieve the same from localStorage. However, I am encountering the following error. Type 'number' is not assignable to type '{ id: string; price: number; ...

Is it possible to identify the form triggering the ajax call within a callback function?

There are multiple forms on my website that share the same structure and classes. The objective is to submit form data to the server using the POST method, and display an error message if any issues arise. Here's how the HTML code for the forms look ...

Using JavaScript to dynamically retrieve element IDs

Within the content on my page, there are multiple tables displaying product information along with their respective authors. Additionally, there is a div that contains hyperlinks for each author. Currently, I am linking the authors' names to hyperlink ...

Allow users to copy and paste on a website where this function is typically restricted

Just wanted to mention that I have very little coding knowledge, so I appreciate your patience. I'm attempting to paste something onto a site that doesn't allow it. Here is the link to the javascript they used to block it: A friend of mine recom ...

I am looking to modify the visibility of certain select options contingent upon the value within another input field by utilizing ngIf in AngularJS

I am currently utilizing Angularjs 1.x to develop a form with two select fields. Depending on the option selected in the first select, I aim to dynamically show or hide certain options in the second select field. Below is the code snippet for the form: &l ...

Utilizing the useEffect hook to display a notification banner when a message is present, along with implementing useState for managing the state of that message

I created a component that displays a Snackbar if a message is not null. I implemented it to show up when someone clicks on the Log In button, and if there is an error, the error message should be displayed in the Snackbar. However, once the user closes th ...

Each styled component will yield the respective type definitions using (@types/styled-components)

Encountering a strange problem with styled-components in VSCode. Every component from styled-components is returning 'any'. I had it working previously, but unsure when it stopped and I can't spot the issue causing all components to return ...

Tips for generating a dynamic Array name to be sorted with React JS

My lack of experience is causing some issues for me. I am currently working on a form in react where the user has to select two values first. Based on these two values, a third value will be available for selection. However, the options for this third val ...

The correct pattern must not be matched by ng-pattern

Within the ng-pattern attribute, we can define a specific pattern for a field to match. Is there a way to indicate that the field should NOT match the specified pattern? For instance: <input type="text" ng-pattern="/[*|\":<>[\]{}`()&a ...

Establishing foreignObject coordinates in Angular

Struggling with setting the position (x, y) for foreignObject in Angular. I have attempted the following: <foreignObject width="65" height="50" x="{{position?.x}}" y="{{position?.y}}"> <div class="c ...

Using JavaScript and node.js, make sure to wait for the response from socket.on before proceeding

My task involves retrieving information from the server on the client side. When a client first connects to the server, this is what happens: socket.on('adduser', function(username){ // miscellaneous code to set num_player and other variabl ...

Converting a LovelySoup AJAX table into JSON Format

Struggling to extract data from a table on a webpage that requires an AJAX call to reveal the information. Unfortunately, upon running my code, I encountered the following error: AttributeError: 'NoneType' object has no attribute 'find_all&a ...

Creating a pop-up effect for a div in the center of a table cell using HTML and CSS

I am currently working with Angular and facing a challenge where I need to display a div like a popup in a table cell when clicked. Despite having the click event logic in place, I am unsure of how to achieve this without using external libraries such as B ...

Empty initial value for first item in array using React hooks

My goal is to store an array that I retrieve from an API in a useState hook, but the first entry in my array always ends up empty. The initial array looks like this: (3) ["", "5ea5d29230778c1cd47e02dd", "5ea5d2f430778c1cd47e02de"] The actual data I recei ...

Transferring information between an ASP web form page and an Angular2 application

Currently, I am working on a project that involves dealing with a legacy WebForms system. The system is gradually being updated to Angular 2, but the transition is happening incrementally. In order to effectively integrate information from the legacy sect ...