Home > Web Front-end > JS Tutorial > body text

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

Mary-Kate Olsen
Release: 2024-11-06 06:39:02
Original
909 people have browsed it

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.
Copy after login

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
      Copy after login
  • Linux/Unix:

    • Open a terminal and run:

      google-chrome --allow-file-access-from-files
      Copy after login
  • 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
      Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!