Created
May 18, 2017 09:33
-
-
Save Papillard/38614a9b029f2f5619721967eafa33f5 to your computer and use it in GitHub Desktop.
POST gist with AJAX
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(){ | |
$("#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); | |
} | |
}); | |
}); | |
}); |
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
<!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