Skip to content

Instantly share code, notes, and snippets.

@mrcampbell
Created May 8, 2017 18:53
Show Gist options
  • Save mrcampbell/4fdab8583d4afcd0b299d106b02b044a to your computer and use it in GitHub Desktop.
Save mrcampbell/4fdab8583d4afcd0b299d106b02b044a to your computer and use it in GitHub Desktop.
Basic demonstration of an Async REST call for a friend.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<style>
#main-wrapper {
margin: 30px;
}
</style>
</head>
<body>
<div class="container" id="main-wrapper">
<div class="row">
<form>
Breed Number:<br>
<input type="text" name="BreedNumber" id="breed-number-input">
<a class="btn btn-default" href="#" role="button" onclick="runProcess()">Submit</a>
</form>
<br>
<div id="breed-name"></div>
</div>
</div>
<script>
function displayResult(result) {
console.log(result);
$("#breed-name").text(result.name)
}
function runProcess() {
console.log("Run Process:")
var breedNumber = fetchBreedInput();
if (breedNumber > 0 && breedNumber < 152) {
url_final = "http://pokeapi.co/api/v2/pokemon/" + breedNumber;
$.ajax({
url: url_final,
async: true, // try playing with this one
success: function(result) {displayResult(result)}
})
}
}
function fetchBreedInput() {
var input = $("#breed-number-input").val();
var inputInt = 0;
try {
// perform validation here
// This is a stupid example, but felt like it was needed
inputInt = parseInt(input);
if(isNaN(inputInt)) {
throw "Number Not Parsed"
}
} catch (ex) {
console.log("ERROR: ", ex)
return -1
}
console.log("Fetch Input Value Successful: ", inputInt)
return inputInt;
}
</script>
</body>
@mrcampbell
Copy link
Author

It's sloppy, I know - but was a quick and dirty solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment