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
MacOS
<code class="pre">open -a 'Google Chrome' --args -allow-file-access-from-files</code>
Linux
<code class="pre">google-chrome --allow-file-access-from-files</code>
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>
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!