Skip to content

Instantly share code, notes, and snippets.

@esironal
Created August 26, 2015 22:36
Show Gist options
  • Save esironal/9fd766c2e1549c60026f to your computer and use it in GitHub Desktop.
Save esironal/9fd766c2e1549c60026f to your computer and use it in GitHub Desktop.
JsDeliver API Test
<!doctype html>
<html><head>
<title>How to Parse a JSON file using jQuery</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<style>
body { text-align: center; font-family: arial; }
.button { margin:20px; font-size:16px; font-weight: bold; padding:5px 10px; }
</style>
</head><body>
<a href="//api.jsdelivr.com/v1/jsdelivr/libraries/lodash?fields=lastversion,name" target="_blank">Open JSON file</a><br />
<input type="button" value="Get and parse JSON" class="button" />
<br />
<span id="results"></span>
<script>
//When DOM loaded we attach click event to button
$(document).ready(function() {
//after button is clicked we download the data
$('.button').click(function(){
//start ajax request
$.ajax({
url: "//api.jsdelivr.com/v1/jsdelivr/libraries/lodash?fields=lastversion,name",
//force to handle it as text
dataType: "text",
success: function(data) {
//data downloaded so we call parseJSON function and pass downloaded data
var json = $.parseJSON(data);
json = json[0]; // un-array responce
$('#results').html('Plugin name: ' + json.name + '<br />Last Version: ' + json.lastversion);
}
});
});
});
</script>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment