Skip to content

Instantly share code, notes, and snippets.

@Papillard
Created May 18, 2017 09:33
Show Gist options
  • Save Papillard/38614a9b029f2f5619721967eafa33f5 to your computer and use it in GitHub Desktop.
Save Papillard/38614a9b029f2f5619721967eafa33f5 to your computer and use it in GitHub Desktop.
POST gist with AJAX
$(document).ready(function(){
$("#submit").on("click", function(){
var content = $("#content").val();
var gistData = {
"description": "Stupid gist",
"public": true,
"files": {
"hello.rb": {
"content": content
}
}
};
// POST request to create my gist
var apiUrl = "https://api.github.com/gists";
$.ajax({
type: "POST",
url: apiUrl,
data: JSON.stringify(gistData),
success: function(data){
console.log(data);
},
error: function(error){
console.log(error);
}
});
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AJAX demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
</head>
<body>
<div class="container text-center">
<h1>Gist creator</h1>
<input type="text" placeholder="enter your gist content" id="content">
<button id="submit">Create gist</button>
</div>
<!-- Including jQuery + Bootstrap + your JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></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