What triggers the invocation of the onerror handler in XMLHttpRequest?

I am facing a bit of confusion when trying to understand the functionality of XMLHttpRequest's handlers. After reading the specification regarding the onerror handler, it mentions:

error [Dispatched ... ] When the request has failed.

load [Dispatched ... ] When the request has successfully completed.

The issue lies in determining what exactly constitutes as "the request has failed". This could mean

  • the request was unable to be sent at all (e.g., Connection refused and similar errors), or
  • the above scenario plus receiving an error code from the server (e.g., 404)

Furthermore, I am curious to know if this implies that onerror and onload should never trigger simultaneously.

According to this resource, the execution of the onerror handler is dependent on the status code, while onload depends on the readyState. Although this suggests they are not mutually exclusive, I am unsure if this information holds authoritative weight.

I am seeking clarification because when using the latest Opera snapshot, I noticed that onload is being triggered even when encountering a 404 status code. While checking the status is a reliable method, I am uncertain whether this aligns with the specifications or if it's simply a workaround for an issue in Opera.

Answer №1

It was noted in the feedback that onerror triggers when there is a breakdown on the connection level. Should the problem be solely at the application level, such as an HTTP error code being transmitted, then onload will still trigger. Make sure to specifically check the status code returned in your onreadystatechange handler.

Keep in mind, a denied cross-domain request will also cause the onerror handler to execute.

Answer №2

Aside from apsillers' response, it's important to remember that XMLHttpRequest automatically handles redirects in the background. This means you won't need to worry about checking for reply codes in the onload event - as this event will only be triggered once on the final call. Additionally, if you're sending payload data with the POST method and the request is redirected, XMLHttpRequest will switch the method from POST to GET and discard any payload data for security purposes. While the onload event will still occur, you'll have to manually resend your request to the new destination.

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

Adjusting the text of a button when hovering over it will also trigger a resizing of the

Currently, I am facing an issue where the bootstrap button's size changes when hovered over. My intention is to have the bootstrap button remain a fixed size while only the text inside it changes using javascript for mouseover and mouseout events. How ...

In Angular JS pagination, the previous filter value is stored in $LocalStorage for future reference

One view displays all order records in a tabular format with 10 records per page. A filter is set to show only paid orders, which pops up filtered data when selected. An issue arises when closing the pop-up window and navigating to the next page of the t ...

What is the best way to conceal a specific class of DIV within a container, and how can I also hide another DIV within the same container by

I have a DIV class containing around 10 elements within a container. I am looking to implement a feature where these elements are hidden and shown one by one every 15 seconds using jQuery with a smooth fadeOut() effect. Your assistance in achieving this wo ...

Ruby on Rails: Making an Easy AJAX Call

i am encountering an issue with my ajax request: within my application, i am attempting to set up a basic ranking system. Once configured, whenever I click on the rank button, the page reloads and the rank is refreshed. I need help understanding how to i ...

Why isn't changing the property of a Sequelize instance having any effect?

While I've successfully used the standard instance syntax in the past, I'm facing a challenge with updating an instance retrieved from the database in this specific section of my code. ... const userInstance = await db.models.Users.findOne({wher ...

Troubleshooting problems with the width of a Bootstrap navbar

I'm encountering a frustrating issue with my bootstrap menu. When I include the "navbar Form" in the menu, the width of the menu extends beyond the screen width in mobile mode (when resizing the browser window or viewing on a mobile device). Interesti ...

The vanishing act: Semantic UI menu disappears when you click

My objective is to create a persistent left-side menu using Semantic-UI. I want to implement two different states for the menu - one with text for each item and another with an image for each item. However, I am facing some challenges that have me complete ...

Exploring Data in Angular 2: Examining Individual Records

I am currently learning Angular and facing some challenges in structuring my questions regarding what I want to achieve, but here is my query. Within a component, I am retrieving a single user record from a service. My goal is to display this user's ...

Implementing relative pathing in front-end development while using ExpressJS for the back-end

I'm currently in the process of developing an application with Express 4.14. When it comes to routing, I have a situation where the incoming request is "https://example.com/page", and I am using res.sendFile(__dirname + "/../client/pages/page/index.ht ...

When attempting to submit data, the Magnific Popup page is restored to its default format

I am facing an issue with my Magnific Popup page: function dataLink(){ $.magnificPopup.open({ items: { src: 'datapage.html', type: 'ajax' }, closeOnContentClick : false, clos ...

Learn how to trigger the keydown function in VUE programming

I am trying to create a method that will be triggered whenever any key on the keyboard is pressed, regardless of where the focus is. I want to be able to determine which key was pressed in this method. Currently, I have set up an event listener for keydow ...

Express.js redirection not refreshing Jade HTML content

I'm currently facing an issue with displaying a flash message in Express.js using Jade's templating engine and connect-flash. My goal is to show an error message when a user tries to add a new User object to the database that already exists. Howe ...

Is the condition failing to evaluate for all td elements?

I am currently dealing with an HTML table. When I select a checkbox, I aim to compare the values of the cells in each row. This comparison works correctly for the first row, but it does not work for any subsequent rows. HTML Code - <form role="fo ...

Tips for keeping JavaScript created checkboxes saved and accessible

Utilizing the "ajax" function within the script enables communication with the server by sending post or delete messages. The javascript code that includes ajax is responsible for dynamically adding checkboxes to the page. How can we ensure that the create ...

Ways to update the component's state externally

I'm new to Next.js (and React) and I'm attempting to update the state of a component from outside the component. Essentially, I am conditionally rendering HTML in the component and have a button inside the component that triggers a function to se ...

"Including Span icons, glyphs, or graphs within a table cell in AngularJS based on a specified condition or numerical

I'm attempting to incorporate span icons/glyph-icons using Angular within a td before displaying the country. I've utilized the code below to achieve this, but I'm looking for a more dynamic and elegant solution. Your assistance is greatly a ...

"What are some best practices for utilizing the $.ajax method in conjunction with the

Utilizing AJAX, I send data to my Apache/PHP webserver (XAMPP). The shorthand syntax $.post works perfectly fine for me. Here is the code snippet: return $.post('http://localhost:8099/login.php', { loginDataInput : loginDataInput }).th ...

Having trouble with my Express.js logout route not redirecting, how can I troubleshoot and resolve it?

The issue with the logout route not working persists even when attempting to use another route, as it fails to render or redirect to that specific route. However, the console.log("am clicked"); function works perfectly fine. const express = require('e ...

When utilizing the .populate() method in Mongoose, how can you retrieve and append ObjectIds with extra attributes to an array (which is returned by .populate()) collected from the

When using the .populate() method in Mongoose, how can I retrieve and add ObjectIds with additional properties to an array (returned by .populate()) if their corresponding document in the collection no longer exists? This question pertains to MongoDB and ...

Error when using jQuery AJAX to parse JSONP callback

Take a look at this code snippet: $(function () { $.ajax({ type: "GET", url: "http://mosaicco.force.com/siteforce/activeretailaccounts", dataType: "jsonp", crossDomain: true, jsonp: "callback", error: f ...