Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin

I have a code.html file containing the following code.

$.ajax({ 
    type: "POST", 
    datatype: "JSONP",
    url: "path",
    success: function(msg){
    var e = document.createElement("div");
    e.id = "ads";
    document.body.appendChild(e);
    $("#ads").html(msg);
    }
});

When I open the code.html file in the browser, it gives an error:

**"XMLHttpRequest cannot load file://..... Origin null is not allowed by Access-Control-Allow-Origin."**

What is causing this and what can I do to fix this?

like image 220
Thasni anes Avatar asked Aug 17 '11 11:08

Thasni anes


People also ask

How do I fix not allowed by Access-Control allow origin?

There Are Two Approaches to Getting It Right.Use a reverse proxy server or WSGI server(such as Nginx or Apache) to proxy requests to your resource and handle the OPTIONS method in the proxy. Add support for handling the OPTIONS method in the resource's code.

How do you solve XMLHttpRequest Cannot load?

To do so, you need to cross domain boundaries. JSONP is really a simple trick to overcome the XMLHttpRequest same domain policy. So, instead of using XMLHttpRequest we have to use < script > HTML tags, the ones you usually use to load JavaScript files , in order for JavaScript to get data from another domain.

How do I allow CORS Access-Control allow origin?

Simply add a header to your HttpServletResponse by calling addHeader : response. addHeader("Access-Control-Allow-Origin", "*");

What does Access-Control allow origin null mean?

Access-Control-Allow-Origin: null This value should not be used to serialize the origin of resources that use a non-hierarchical scheme. Sandboxed documents are defined as null. User agents may grant access to these documents and create a hostile document with null origin.


1 Answers

I will make two assumptions:

  • You are probably using chrome
  • You are opening a file from the filesystem (i.e. double clicking)

Then, this question is a duplicate of XMLHttpRequest Origin null is not allowed Access-Control-Allow-Origin for file:/// to file:/// (Serverless)

The browser is preventing cross site scripting. See: https://developer.mozilla.org/en-US/docs/HTTP_access_control

like image 185
Josep Valls Avatar answered Oct 01 '22 23:10

Josep Valls