0

Here is my working example trying to load a file from github:

<html>
<head><title>Get Gists</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
    var url =  "https://gist.github.com/2406934.js?file=check-jquery-load";
    $.getJSON(url + "?callback=?", null, function(gist) {
        alert(gist);
        $("#gist-info").append(gist);
    });
});
</script>
<div id='gist-info'></div>
</body>
</html>
1

2 Answers 2

1

You pretty much have 2 issues here. Cross-domain and the fact that the fragment that's being returned is not exactly a JSON. It's in between an HTML and a Javascript.

You definitely need a helper method to parse the returned object since it's performing a document.write().

See the following link which pretty much solves the same issue that you're having: Loading GitHub Gist

This is what your Gist looks like in action when put together: Link

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the Solution Dennis. before you wrote this I re-worked my code to use fancybox to load the gist embeded code into an iframe by passing the gist id and filename in the querystring to the page loaded in the fancybox iframe. If I decided to re-work this though this looks like a great option too.
1

That URL is not returning valid JSON.

From the jQuery Documentation:

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.

3 Comments

oh...I thought it might be possible to access non-json data like this. I guess I need to go down the route of a proxy then instead?
Here is a similar question/answer to what you are looking for: stackoverflow.com/questions/1292486/…
Here is another answer with some jQuery plugins that will allow you to do a cross-domain load: stackoverflow.com/questions/5320511/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.