Synchronous CORS XHR encounters issues with 302 redirect, while asynchronous function successfully redirects

Why does Firefox seem to be the only browser that doesn't throw an error when performing a synchronous request? Any insights on this peculiar behavior?

// Ensure your JS console is open when running this script
var url = '//api.soundcloud.com/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=83f67039ae0c3790030d256cb9029678';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false); // SYNCHRONOUS
xhr.onload = alert.bind(null, 'Loaded');
xhr.send(null);

When performing a similar asynchronous XMLHttpRequest, no errors are thrown, and the request executes as expected:

// Make sure to have your JS console open when you run this
var url = '//api.soundcloud.com/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=83f67039ae0c3790030d256cb9029678';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true); // ASYNCHRONOUS
xhr.onload = alert.bind(null, 'Loaded');
xhr.send(null);

Based on my understanding:

  • The SoundCloud API returns relevant CORS headers.
  • The SoundCloud resolve API initiates a 302 redirect to
    http://api.soundcloud.com/tracks/49931.json
    .
  • If the request is conducted asynchronously, it navigates through the redirect successfully and completes.
  • If the request is executed synchronously, it fails with various errors in browsers like Chrome, Opera, and Safari, but not in Firefox. The errors include:
    • Chrome: "NetworkError: A network error occurred."
    • Opera: "NetworkError: A network error occurred."
    • Safari: "XMLHttpRequest Exception 101: A network error occurred in synchronous requests."

Can someone clarify why the synchronous request fails? What causes this discrepancy in behavior between synchronous and asynchronous requests? Is it attributed to a specific bug in Firefox or in the WebKit/Blink engines?

Edit: I've reported an issue on Chrome's bug tracker as there seems to be no definite explanation for this behavior in specifications. A similar issue may be raised for WebKit in due course.

Answer №1

It appears that a deliberate decision was made to discourage the use of synchronous requests:

It seems like IE never supported it in the first place, from what I gather.

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

Manipulating Hyperlinks outside the Angular JS applicationIn certain cases, Angular JS

This is my first attempt at creating a single page app. The app's content is contained within the page like a widget, functioning similar to pagination. In the header, I have links that are not related to the angular app. The link structure looks lik ...

Displaying unique input values with ng-model

Within the controller, there is a variable that monitors the page index (starting at 0) for a paginated table: var page { pageNumber: 0; } Query: How can I display this pageNumber variable in the HTML, but always incremented by +1? (since the index=0 p ...

When Safari injects elements that were previously hidden with display:none, they remain visible

Using the JS library MagnificPopup, I implement popups on my website triggered by a "read more" button. This library moves the html to display it in a different location and then injects it back when the popup is closed. While this functionality works seam ...

Failure to display alert message upon completion of AJAX request

I am attempting to use AJAX to send data to the database without refreshing the page when a user favorites a message. Even though the data is successfully sent to the DB, the page still reloads and the alert message I want to display is not showing up. Th ...

Obtaining Input in NodeJS

I am working on a code that captures user input but I am facing an issue. Currently, the code does not wait for the input to be completed. I am looking for a solution where the code will pause until the user has entered their input. It is crucial for me th ...

successful callback after passport registration

router.post('/register', function(req, res, next){ var name = req.body.name; var email = req.body.email; var username = req.body.username; var password = req.body.password; var password2 ...

Tips for setting a new key and value for an existing object in TypeScript

As I transition from JavaScript to TypeScript, I am currently working on creating a Discord bot using TypeScript to familiarize myself with the environment. However, I encountered an error when attempting to add new keys to an object that was previously cr ...

Unable to change the value of Apexcharts components

Utilizing the Vue.js framework along with the Apexchart library, I have been able to create scatterplots for my data. By making use of Axios, I can successfully transmit my data and monitor it using console.log(). With the help of Apexcharts property updat ...

React TextField is not accommodating the new line character ' ' causing recognition issues

Explanation I have encountered an issue while using Material UI TextField and mapping through an array of objects fetched from a MongoDB database. Here is the code snippet in question: {state.map((item) => ( <TextField name=" ...

What steps can I take to resolve the issue of item display in the dropdown menu?

My objective is to have a dropdown list with "title 1234" appearing first. When this option is selected, its value should be 1234. How can I achieve this? Here is the desired outcome: https://i.sstatic.net/CqWgn.png Currently, the list looks l ...

What is the method for asynchronously loading a javascript file that includes an ajax call?

Within my JavaScript file named JScript.js, there is a function that includes an AJAX call to a dot-net page. alert('jscript.js called'); function AddTag() { var htag = document.getElementById("hdntag").value.split('|'); var texttag = ...

Tips for eliminating the glowing effect when pressing down on the thumb of the material ui slider

Visit Material-UI documentation for Slider component. Despite setting the disabled flag for the entire slider, the thumb continues to show a halo effect when clicked. ...

JavaScript error: Function is not defined when using Paper.js

UPDATE the problem has been solved by making the colorChange function global I am attempting to modify the color of the path when the 'Red' button is clicked using the colorChange function. Despite my efforts, I keep getting an error stating tha ...

Struggling to implement the UI router into my Angular Framework

I've been working on a framework that is supposed to be router agnostic. While I've managed to make it work with ngRoute, I just can't seem to get it functioning with UI Router. Here's a snippet of the main app module: (function () { ...

Updating textbox values with ajax results in the page refreshing and the newly assigned values being lost

I'm currently working on updating a section of my webpage using AJAX instead of C#, as I don't want the page to refresh. All I need to do is execute a SELECT query to retrieve the current client from the SQL database and populate the correspondin ...

The Nuxt.js authentication module remains in place and does not redirect users who are already logged

An authenticated user can still access the /login page. If I click on a link to the /login page, I am redirected to a different page successfully. However, if I manually type in /login in the URL, I am still taken to the login page even though I am already ...

Grouping an array of arrays of objects

I am trying to group an array of objects based on the value of the first item below: const data = [{"value":"value1","metric":1},{"value":"value1","metric":2},{"value":"value3","metric":0},{"value":"value2","metric":4},{"value":"value3","metric":1},{"va ...

What could have caused these errors, since they never made an appearance?

'Link' component cannot be utilized within JSX. The type 'ForwardRefExoticComponent<LinkProps & RefAttributes<HTMLAnchorElement>>' is not a valid element for JSX. The type 'ForwardRefExoticComponent<LinkPro ...

What is the best way to eliminate a CSS style from a div?

I have implemented jQuery Autosize to automatically adjust the height of textarea elements. It works perfectly when I focus on the textarea element. However, when I blur out of the textarea, I want to reset the height to its default value. I am unsure of ...

``I am experiencing slow loading times with Google Maps JS when using

When accessing Google Maps on Safari, zooming in can be frustratingly slow on both Windows and Mac. The experience feels glitchy, with the zoom starting, freezing, then finally finishing. In comparison, Chrome provides a much smoother and faster zooming ex ...