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

2

9

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?

Berners answered 17/8, 2011 at 11:26 Comment(5)
Is your code.html on the same host (localhost)?Breeks
in same host its works fine. but in different its not workingBerners
possible duplicate of XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-OriginBreeks
possible duplicate of XMLHttpRequest Origin null is not allowed Access-Control-Access-Allow for file:/// to file:/// (Serverless)Stavro
this is very comprehensive 3 simple solutionsJumpoff
E
9

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

Enrobe answered 29/10, 2012 at 18:48 Comment(0)
S
2

if you your dataType is jsonp(lowercased), the ajax type must be GET not POST

Update:

Use $.getJSON insteadof $.ajax should solve your problem

Schematic answered 17/8, 2011 at 11:38 Comment(2)
what's the address you posted?Schematic
use $.getJSON insteadof $.ajaxSchematic

© 2022 - 2024 — McMap. All rights reserved.