Home  >  Article  >  Web Front-end  >  Why Does My Same-Origin AJAX Request Fail with a 'Cross Origin Request Error'?

Why Does My Same-Origin AJAX Request Fail with a 'Cross Origin Request Error'?

DDD
DDDOriginal
2024-11-06 06:06:02255browse

Why Does My Same-Origin AJAX Request Fail with a

Cross Origin Request Error: File Fetch Fails for Same-Origin Request

When attempting to make an AJAX request to a local webserver file using jQuery, developers may encounter the following error: "XMLHttpRequest cannot load file:///C:/xampp/htdocs/webname/resources/templates/signup.php. Cross origin requests are only supported for HTTP."

Despite the error message suggesting a cross-domain request issue, in this case, the request is for a local file hosted on the same web server as the website.

Solution: Allow File Access via Chrome Switch

To resolve this problem, developers can utilize a Chrome command-line switch to allow file access from local files.

  1. For OS X:

     open -a 'Google Chrome' --args -allow-file-access-from-files
  2. For *NIX:

    google-chrome --allow-file-access-from-files
  3. For Windows:
    Edit the target path of the Chrome shortcut, adding the following switch:

    C:\ ... \Application\chrome.exe --allow-file-access-from-files

The above is the detailed content of Why Does My Same-Origin AJAX Request Fail with a 'Cross Origin Request Error'?. 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