Background:
When attempting to make AJAX requests using the $.get() method to files hosted on a local web server, you may encounter the following error in the Google Chrome console:
XMLHttpRequest cannot load file:///.... Cross origin requests are only supported for HTTP.
This error arises despite the absence of cross-domain issues, as the request is made from the same server that hosts the website.
Solution:
To resolve this issue, you can bypass the browser's cross-origin protection by starting Google Chrome with the --allow-file-access-from-files switch. This switch allows Chrome to access files from the local filesystem.
Instructions:
macOS:
Open Terminal and type:
open -a 'Google Chrome' --args -allow-file-access-from-files
Linux/Unix:
Open a terminal and run:
google-chrome --allow-file-access-from-files
Windows:
In the "Target" field, add the switch to the end of the path, e.g.:
C:\ ... \Application\chrome.exe --allow-file-access-from-files
The above is the detailed content of How to Enable Cross-Origin Requests for Local Files in Google Chrome?. For more information, please follow other related articles on the PHP Chinese website!