Home >Web Front-end >JS Tutorial >How to Fix \'XMLHttpRequest cannot load...requests are only supported for protocols: http, data...\' CORS Errors in Angular and Express?

How to Fix \'XMLHttpRequest cannot load...requests are only supported for protocols: http, data...\' CORS Errors in Angular and Express?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-02 19:32:38256browse

How to Fix

CORS Error: "XMLHttpRequest cannot load...requests are only supported for protocols: http, data..."

Problem:

When attempting to access a JSON string via Angular Service from an Express backend, users may encounter the following CORS error: "XMLHttpRequest cannot load localhost:4201/ticker. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https."

Answer:

This error occurs when using an incorrect URL protocol in the request. To resolve it, ensure the protocol is explicitly specified in the URL.

In this specific case, the solution is to add "http://" before the localhost address in the Express backend, as follows:

this._baseUrl = 'http://localhost:4201/';

This ensures that the correct URL format is used to fulfill the request and resolve the CORS error.

The above is the detailed content of How to Fix \'XMLHttpRequest cannot load...requests are only supported for protocols: http, data...\' CORS Errors in Angular and Express?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn