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?
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.
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.
Simply add a header to your HttpServletResponse by calling addHeader : response. addHeader("Access-Control-Allow-Origin", "*");
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.
I will make two assumptions:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With