Significant issue identified with ajax functionality, requiring immediate attention

After executing the code in the console, I expected to see the ajax object with a readyState of 0 (as I aborted the ajax call). However, Chrome is displaying the number 4 in the console instead of 0. This discrepancy is surprising.

http://jsfiddle.net/8yCfn/1/

temp = new XMLHttpRequest();

temp.open("POST","http://jsfiddle.net/",true);

temp.onreadystatechange = function() {

    console.log(temp);
    console.log(temp.readyState);

};

temp.send(null);

temp.abort();

Can someone help resolve this issue with Chrome? I plan to report it as a bug but would appreciate insights from those more knowledgeable than myself.

--

ATTENTION:

It seems my question went unanswered.

temp = new XMLHttpRequest();

temp.open("POST","http://jsfiddle.net/",true);

temp.onreadystatechange = function() {

console.log(temp);
console.log(temp.readyState);

};

temp.send(null);

temp.abort(); Why does the readyState property of the temp object show differently when printed in the console? It should be consistent, regardless of browser behavior.

If an object's property xxx is set to 0, then object.xxx MUST be 0, as illustrated by this example showing the bug.

Answer №1

If you wish to view the readyState zero [unset], it must be done outside of the onreadystatechange handler and prior to triggering open.

temp = new XMLHttpRequest();
temp.onreadystatechange = function() {
    console.log(temp.readyState);
};
console.log(temp.readyState);
temp.open("POST",window.location.href,true);
temp.send(null);

The console output will be as follows:

0 
1 
2 
3 
XHR finished loading: "http://fiddle.jshell.net/_display/".
4

Now, let's consider aborting

temp = new XMLHttpRequest();
temp.onreadystatechange = function() {
    console.log("orsc:", temp.readyState);
};
console.log("Before Open: ", temp.readyState);
temp.open("POST",window.location.href,true);
temp.send(null);
temp.abort();
console.log("After abort: ", temp.readyState);

Console output will show:

Before Open: 0 
orsc: 1 
orsc: 4 
After abort: 0 

This behavior aligns with what is specified in the final steps of the Spec

6. If the state is UNSENT, OPENED with the send() flag being unset, or DONE, proceed to the next step.
   Otherwise, follow these substeps:
        Change the state to DONE.   <---- Set to 4
        Unset the send() flag.
        Fire an event named readystatechange.    <---- Event fired onreadystate change
        ...
7. Reset the state to UNSENT.      <---- Set to 0
    No readystatechange event is triggered.

In simpler terms, it instructs to set the ready state to 4 and trigger onreadystatechange. Then, it directs to reset the ready state to zero without invoking a new onreadystatechange. This explains why the example displays a 4 followed by a 0.

Before Open: 0 
orsc: 1 
orsc: 4          <--Abort step 6 - set to done, fire readystatechange
After abort: 0   <--Abort step 7 - set to zero, don't fire readystatechange 

An Explanation Regarding Console Behavior

You mentioned:

It does not clarify why console.log(a) displays "readyState"=>0 while console.log(a["readyState"]) shows 4.

This discrepancy occurs because console.log(a) refers to the object itself! The console doesn't show the value at recording time but when viewed, adjusting dynamically!

A simple browser demo illustrates this point clearly.

JSFiddle: http://jsfiddle.net/4RDh3/

Answer №2

In various other browsers as well, the XMLHttpRequest can exhibit similar behavior. According to the specification, when the request is aborted, the readyState property transitions to 4 before reverting back to 0.

This means that even if a request is aborted, checking for the onchange event may still return a readyState value of 4.

You can determine if the user specifically aborted the request by using the following expression:

!xhr.getAllResponseHeaders();

Where xhr represents the XMLHttpRequest object, and this expression will evaluate to true if the request was indeed terminated by the user.

The reality is that browsers may not strictly adhere to the specifications. While it states that the readyState should be set to 0 when the user aborts, each browser implementation may introduce its own quirks before eventually complying with the specified behavior.

Answer №3

Seems like my question went unnoticed.

temp = new XMLHttpRequest();

temp.open("POST","http://jsfiddle.net/",true);

temp.onreadystatechange = function() {

    console.log(temp);
    console.log(temp.readyState);

};

temp.send(null);

temp.abort();

Why is it that in the first line of the console, the object temp shows readyState property as 0, but then in the second line it displays 4? It's confusing that they are different despite consistent browser behavior.

If an object has a property xxx set to 0, then object.xxx MUST be 0 - not 4 as demonstrated by this bug in my example.

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

Differences: Angular ngController vs Controller embedded in Directive

I'm interested in exploring the various scenarios where these two methods of creating a controller can be used: Using ngController: myApp.controller('myController', ['$scope', function ( $scope ) { }]); Creating the controller ...

What is the best way to combine key-value pairs objects into a single object using JavaScript?

I am faced with the challenge of creating a new object that combines keys from a specific array (const props = []) and values from existing objects. If a key does not exist in an object, I aim to populate it with null or placeholder values. Here is my cur ...

Learn the technique of coding HTML within inline JavaScript, along with implementing CSS inline styling

I'm looking for a way to incorporate HTML within inline JavaScript, along with CSS inline styles. Can someone provide guidance on how to achieve this? For example, something like the following code snippet: <p><span style="color: #00ff00;"&g ...

Issues encountered when making ajax requests within a Phonegap mobile application

I'm currently working on building a basic RSS reader using Phonegap and jQuery. I found this helpful tutorial: . Initially, everything was functioning correctly when testing the code in my browser. The php-file successfully retrieved and displayed th ...

The `this` value is null within an onClick function of a ReactJS component

The issue occurs with the onClick method of <span className="nav-toggle">. The specific error message is: cannot read property setState of null. It seems to be related to the scoping of this or due to the asynchronous nature of the setState method. ...

Attempting to modify PHP query following submission of data via Axios in Vue application

Fetching a JSON object through a PHP query from a service known as BullHorn can be done like this: <?php echo getBhQuery('search','JobOrder','isOpen:true','id,title,categories,dateAdded,externalCategoryID,employmentTy ...

Issue: $controller:ctrlreg The controller named 'HeaderCntrl' has not been properly registered

I am encountering an error while working on my AngularJS program. I have defined the controller in a separate file but it keeps saying that the controller is not registered. Can someone please help me understand why this issue is happening? <html n ...

React's createPortal function does not place the child element inside a container

My new to-do list app in React has a simple functionality where clicking a button should add a new item to the list. However, I am facing an issue with the createPortal method not triggering the UI to render the new item. Here's the code snippet causi ...

The data values are transformed following a JSON AJAX POST request with a percentage symbol

I'm facing an issue while developing a slide editor using AJAX. I have a PHP script with multiple functions that can be accessed via different GET parameters. Everything works smoothly until I try to save the slides, at which point the value of transf ...

Callback not being triggered after AJAX request

I am currently troubleshooting some code that was not written by me, and I am facing challenges in understanding why an Ajax return is not triggering a Callback. The following code is responsible for attaching behaviors to the ajax functions: # Callback ...

searching for a refined method to tackle this challenge

I'm relatively new to Angular and I'm interested in finding a more efficient solution for this particular task. The code below functions correctly, but I am uncertain if it is the best approach to achieve the desired outcome. HTML <div ng-a ...

Adjust the opacity of a div's background without impacting the children elements

I am currently working on a dynamic content display in my HTML page using some knockout code. The setup looks like this: <section id="picturesSection" class="background-image" data-bind="foreach: { data: people, as: 'person'}"> <div ...

The flow of events is not hindered by an if statement, even when the code within it is executed

I'm facing an issue where the console.log statement keeps executing even after calling the search function within the "if statements" in my code. Is there a way to prevent this from happening? function search() { /** * The Tweet checking algori ...

How is it that connecting an event listener to a React child component can be done in a way that resembles simply passing a prop

class Board extends React.Component { constructor(props) { super(props); this.state = { squares: Array(9).fill(null), }; } handleClick(i) { // do things } render() { return ( <div ...

Can VueJS lifecycle hooks be outsourced?

Is it possible to organize lifecycle hooks (like created / mounted) in a separate file for better simplicity and cleanliness? MyGreatView.vue import created from 'created.js' export default { created, // created() { console.log('vue Cre ...

Ways to retrieve a specific Array[property] within an object?

I am struggling to access a specific property within an array of objects. My goal is to extract the "name" elements from the app catalog array, combine them with the names from the custom array apps.name, and assign the result to a new property in the ques ...

Using JavaScript, extract current date from an API data

Here is an example of how the data from my API appears: const data = [{ "id": "1", "name": "Lesley", "creationDate": "2019-11-21 20:33:49.04", }, { "id": "2", "name": "Claude", "creationDate": "2019-11-21 20:33:09.397", }, { "i ...

What is the best way to retrieve a specific object from a JSON file using a Get request in a Node.js application?

My focus is on optimizing an API, which is why I'm working with only the data that's essential for my analysis. I've set up a route to extract specific objects, but I'm only interested in four of them: account_manager, fronter, closer, ...

Is there a way to resolve the issue of my localStorage getting overridden on page refresh?

I am currently working on a simple React app and encountering an issue with the localStorage. Whenever I refresh the page, the todo list stored in the localStorage disappears. How can I ensure that the todos remain visible even after a refresh? Any sugges ...

Having issues with the functionality of the Previous/Next button in my table

I am facing a challenge with my table as I am trying to include previous/next button for users to navigate through it. However, the interaction doesn't seem to be functioning properly, and I suspect that I need to establish a connection between the bu ...