-1

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?

Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
jeno
  • 13
  • 6
  • Does this answer your question? [CORS issue in Jquery with GET method](https://stackoverflow.com/questions/44700528/cors-issue-in-jquery-with-get-method) – Masood Mar 14 '21 at 11:49

1 Answers1

2

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>
charlietfl
  • 170,828
  • 13
  • 121
  • 150