简体   繁体   中英

AJAX Https request showing CORS error. (From local server)

Following is the code I tested.
Code:

$.ajax({
  url : 'https://github.com/sab99r/Indian-States-And-Districts/blob/master/states-and-districts.json',
  type : 'GET',
  datatype : 'json',
  header: {
    'Access-Control-Allow-Origin': 'https://github.com'
  }
  .done(function(response)){
    data = JSON.parse(response);
    loadlog(data);
  }
});

Following is the error I am receiving when running the above code.
Error:

Access to XMLHttpRequest at 'https://github.com/somelink/data.json' from origin 'http://127.0.0.1:3000' \ has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have looked into many resources, also.
Resource #1
Resource #2

Can somebody explain the reason for the above error, please?

Go to that url and click on Raw button and use the resulting raw.githubusercontent.com url which is CORS enabled

 $.getJSON('https://raw.githubusercontent.com/sab99r/Indian-States-And-Districts/master/states-and-districts.json').then(console.log)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM