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?

mjk
  • 2,443
  • 4
  • 33
  • 33
Thasni anes
  • 91
  • 1
  • 1
  • 5
  • Is your code.html on the same host (localhost)? – Maerlyn Aug 17 '11 at 11:43
  • in same host its works fine. but in different its not working – Thasni anes Aug 17 '11 at 11:45
  • possible duplicate of [XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin](http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin) – Maerlyn Aug 17 '11 at 13:03
  • possible duplicate of [XMLHttpRequest Origin null is not allowed Access-Control-Access-Allow for file:/// to file:/// (Serverless)](http://stackoverflow.com/questions/4208530/xmlhttprequest-origin-null-is-not-allowed-access-control-access-allow-for-file) – kapa Oct 29 '12 at 23:08
  • this is very comprehensive [3 simple solutions](http://stackoverflow.com/a/15747224/1140227) – George Aug 23 '13 at 09:42

2 Answers2

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

Community
  • 1
  • 1
Josep Valls
  • 5,483
  • 2
  • 33
  • 67
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

wukong
  • 2,430
  • 2
  • 26
  • 33