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?

5

2 Answers 2

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

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

2
  • what's the address you posted?
    – wukong
    Aug 18, 2011 at 7:25
  • use $.getJSON insteadof $.ajax
    – wukong
    Aug 18, 2011 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.