Austin
Austin

Reputation: 3080

Trouble getting JSON from Github

I am still learning how to pull information cross-domain, let alone AJAX/JSON and I am having trouble getting a JSON file from github. (never done this before)

I have been reading about security issues with cross-domain and I am trying to fix this by using header or jsonp but nothing seems to be working.

Here is my attempt thus far (new to this, sorry if it is a bad attempt)

$( document ).ready(function() {
   var ID = "aanders50/0c190f846790c9b05691";
   $.ajax({
       url: 'https://gist.github.com/'+ID,
       type: 'GET',
       dataType: 'json',
       data: { 
       },
       headers: {
           'Access-Control-Allow-Origin': '*'
       },
       success: function (json) {
           alert(JSONurl = 'https://gist.github.com/' + ID);
           alert(json[ID].id);
           alert(json[ID].name);
           alert(json[ID].summonerLevel); 
       },
       error: function (XMLHttpRequest, textStatus, errorThrown) {
           alert("error");
       }
   });
});

I have been searching for awhile now but I feel like I am missing something that I cannot find, which makes it hard to actually search for an answer.. :(

The errors enter image description here

XMLHttpRequest cannot load https://gist.github.com/aanders50/0c190f846790c9b05691. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:56087' is therefore not allowed access.

GET http://localhost:56087/1f950c50642c41cfa5690a661fc548db/arterySignalR/ping?…http%3A%2F%2Flocalhost%3A56087%2FEvents&browserName=Chrome&_=1405009417830 404 (Not Found) 

The Git File enter image description here The File

Any help appreciated!

Upvotes: 0

Views: 267

Answers (2)

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

Your file https://gist.github.com/aanders50/0c190f846790c9b05691 needs to support jsonp and return a JSON response for you to use it outside this domain.

Simply adding headers to your AJAX request will not help here.

Upvotes: 0

Quentin
Quentin

Reputation: 944443

I have been reading about security issues with cross-domain and I am trying to fix this by using header or jsonp but nothing seems to be working.

Access-Control-Allow-Origin is a response header, not a request header. It would be rather pointless if you could grant yourself permission to read data from a server. You can't set response headers for the URL you are using because you don't control the server; Github do.

You can't use JSONP. The document you are requesting is an HTML document, not a JSONP script. (Note that this also means you can't parse it as JSON).

If you want to fetch the page, then you'll need to use a proxy (e.g. make the Ajax request to your own server and have it fetch the data for you).

Upvotes: 1

Related Questions