Skip to content

Instantly share code, notes, and snippets.

@raf-sh
Last active May 29, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raf-sh/df9d5a3a140801d851063dce96e0b681 to your computer and use it in GitHub Desktop.
Save raf-sh/df9d5a3a140801d851063dce96e0b681 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Api tester</title>
</head>
<div id="login">
<form id="formLogin">
Login:<br>
<input type="text" name="login" value=""><br>
Password:<br>
<input type="text" name="password" value=""><br><br>
<input type="submit" value="Submit">
</form>
</div>
<div id="requester">
<button id="sendApiCall"></button>
</div>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var url = "url";
var store = store || {};
/*
* Sets the jwt to the store object
*/
store.setJWT = function(data){
this.JWT = data;
}
/*
* Submit the login form
*/
$("#formLogin").submit(function(e){
e.preventDefault();
$.ajax({
url: 'api/authentificate',
beforeSend: function(request){
},
type: 'POST',
success: function(data) {
store.setJWT(data.JWT);
console.log(store);
$("#login").hide();
},
error: function() {
alert(data);
}
});
});
/*
* Send api call
*/
$("#sendApiCall").click(function(e){
e.preventDefault();
$.ajax({
url: '/api/campaigns',
beforeSend: function(request){
request.setRequestHeader('Authorization', 'Bearer ' + store.JWT);
},
type: 'GET',
success: function(data) {
console.log(data);
// Decode and show the returned data nicely.
},
error: function() {
alert(data);
}
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment