Home > Web Front-end > JS Tutorial > Why Is My Local AJAX Request Throwing a 'Cross-Origin Requests are Only Supported for HTTP' Error?

Why Is My Local AJAX Request Throwing a 'Cross-Origin Requests are Only Supported for HTTP' Error?

Susan Sarandon
Release: 2024-11-06 01:19:02
Original
500 people have browsed it

Why Is My Local AJAX Request Throwing a

XMLHttpRequest Cross-Origin Error

When attempting to execute AJAX requests from a local web server, developers may encounter the error "Cross origin requests are only supported for HTTP." This error occurs despite the request not being cross-domain.

Cause

This error is triggered by the browser's security policy, which restricts cross-origin communication between different domains and protocols for security reasons. However, in this case, the request is being made from a local server to a local resource, making it essentially same-origin.

Solution

To resolve this issue, Chrome users can start the browser with the following switch:

--allow-file-access-from-files
Copy after login

MacOS

<code class="pre">open -a 'Google Chrome' --args -allow-file-access-from-files</code>
Copy after login

Linux

<code class="pre">google-chrome  --allow-file-access-from-files</code>
Copy after login

Windows

Add the switch to the end of the "target" path in the properties of the Chrome shortcut:

<code class="pre">C:\ ... \Application\chrome.exe --allow-file-access-from-files</code>
Copy after login

By enabling this switch, Chrome allows AJAX requests to access local files and resources, resolving the error "Cross origin requests are only supported for HTTP but it's not cross-domain."

The above is the detailed content of Why Is My Local AJAX Request Throwing a 'Cross-Origin Requests are Only Supported for HTTP' Error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template