Created
April 13, 2017 09:03
-
-
Save Papillard/2e8446fa1d3e64b246ff7c46827ef506 to your computer and use it in GitHub Desktop.
Playing with AJAX and Github
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
var baseUrl = "https://api.github.com"; | |
$("#fetch").on("click", function(){ | |
// get the value from username input | |
// make a GET request to api.github.com/users/username/repos | |
// inject response in the list #repos | |
var userName = $("#username").val(); | |
var reposUrl = baseUrl + "/users/" + userName + "/repos"; | |
$.ajax({ | |
type: "GET", | |
url: reposUrl, | |
success: function(data) { | |
$("#repos").empty(); | |
data.forEach(function(repo){ | |
var listItem = "<li>" + repo.full_name + "(" + repo.created_at + ")</li>" | |
$("#repos").append(listItem); | |
}); | |
}, | |
error: function(jqXHR) { | |
$("#repos").empty(); | |
$("#repos").append("<li>" + jqXHR.responseText + "</li>") | |
} | |
}); | |
}); | |
$("#create").on("click", function(){ | |
var gistContent = $("#content").val(); | |
var gistUrl = baseUrl + "/gists"; | |
var gist = JSON.stringify({ | |
"description": "A ruby gist from shitty interface", | |
"public": true, | |
"files": { | |
"hello.rb": { "content": gistContent } | |
} | |
}); | |
$.ajax({ | |
type: "POST", | |
url: gistUrl, | |
data: gist, | |
success: function(data) { | |
$("#content").val(""); | |
$("#gist-url").html("Find your gist at <a href='" + data.html_url + "' target='_blank'>this URL<a>"); | |
}, | |
}); | |
}) | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Playing with AJAX</title> | |
</head> | |
<body> | |
<h1>Gist Creator</h1> | |
<textarea type="text" id="content" placeholder="write ruby code here"></textarea> | |
<br> | |
<button id="create">Create</button> | |
<div id="gist-url"> | |
</div> | |
<script | |
src="https://code.jquery.com/jquery-3.2.1.min.js" | |
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" | |
crossorigin="anonymous"></script> | |
<script src="app.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Playing with AJAX</title> | |
</head> | |
<body> | |
<h1>Fetch Repos</h1> | |
<input type="text" id="username"> | |
<button id="fetch">Fetch</button> | |
<ul id="repos"> | |
</ul> | |
<script | |
src="https://code.jquery.com/jquery-3.2.1.min.js" | |
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" | |
crossorigin="anonymous"></script> | |
<script src="app.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment