XMLHttpRequest cannot load Origin null not allowed 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."** 

Please help me! how to avoid this problem

+8
javascript php
source share
2 answers

I will make two assumptions:

  • Maybe you use chrome
  • You open a file from the file system (i.e. double click)

Then this question is a duplicate of XMLHttpRequest Origin null is not allowed by Access-Control-Allow-Origin for file: /// for file: /// (without server)

Cross-site scripting is prevented in the browser. See: https://developer.mozilla.org/en-US/docs/HTTP_access_control

+7
source

if your data type is jsonp (lower case), the ajax type should be GET not POST

Update:

Use $ .getJSON instead of $ .ajax to solve your problem

+2
source

All Articles