Home  >  Article  >  Web Front-end  >  How to Enable Cross-Origin Requests for Local Files in Google Chrome?

How to Enable Cross-Origin Requests for Local Files in Google Chrome?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-06 06:39:02910browse

How to Enable Cross-Origin Requests for Local Files in Google Chrome?

XMLHttpRequest Cross-Origin Requests for Local Files

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:

    • Right-click on the Google Chrome shortcut and select Properties.
    • 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!

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