Home > Web Front-end > JS Tutorial > How to Fix the 'Origin null is not allowed by Access-Control-Allow-Origin' Error from File:// URLs?

How to Fix the 'Origin null is not allowed by Access-Control-Allow-Origin' Error from File:// URLs?

Barbara Streisand
Release: 2024-12-19 11:54:10
Original
547 people have browsed it

How to Fix the

Resolving "Origin null is not allowed by Access-Control-Allow-Origin" Error for Requests Made from File:// URLs

The error "Origin null is not allowed by Access-Control-Allow-Origin" occurs when a browser restricts cross-origin requests made from files stored locally (file:// URLs). In this scenario, accessing images from Panoramio via jQuery's AJAX is hindered.

To resolve this issue, consider the following:

1. Ensure JSONP Request Type:

Make sure you are using the correct request type for JSONP (JavaScript Object Notation with Padding). jQuery's $.get method default is "json," but for JSONP, it should be "jsonp." You can achieve this in two ways:

  • Use $.getJSON instead of $.get.
  • Add callback=? to the URL when using $.get, which triggers jQuery to use JSONP.

Example using $.getJSON:

$.getJSON(url, function (data) { ... });
Copy after login

Example using $.get with callback=? URL:

$.get(url + "&callback=?", function (data) { ... });
Copy after login

2. Troubleshooting Tips for CORS (Cross-Origin Resource Sharing):

  • Check that the cross-origin request is being made from a valid source (e.g., http://, not file://).
  • Verify browser support for CORS. Internet Explorer and Opera have limited CORS support.

Note: When attempting CORS requests from file:// URLs, the browser may not be able to send an Origin header, resulting in a null origin that the server cannot authorize.

By following these suggestions, you can effectively resolve the "Origin null is not allowed by Access-Control-Allow-Origin" error when making requests from file:// URLs.

The above is the detailed content of How to Fix the 'Origin null is not allowed by Access-Control-Allow-Origin' Error from File:// URLs?. 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