<div class="navbar-wrapper">
<div class="container">
<nav class="navbar navbar-inverse navbar-static-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">South Lake Tahoe's Weather Alerts</a>
</div>
</div>
</nav>
</div>
</div>
<!-- Carousel
================================================== -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<!-- indicators get placed her by js -->
</ol>
<div class="carousel-inner" role="listbox">
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div><!-- /.carousel -->
<div class="container marketing">
<div class="row featurette">
<div class="col-md-7">
<h2 class="featurette-heading">Consuming RESTful API <span class="text-muted">Example Code with JavaScript, jQuery, Bootstrap</span></h2>
<p class="lead">In this example, I take the data from the weather API and display it on the page. I use JavaScript to get the data from the API, and use a combination of jQuery and Bootstrap to put data into DOM. Take js code has comments every step of the way so you'll be able to see what I've done here.</p>
<p>If you're just getting started with REST, here is a good <a href="http://g-liu.com/blog/2016/07/a-strategy-for-consuming-rest-apis-in-javascript/">tutorial on Consuming RESTful APIs</a> by Geoffrey Liu. Start with plain JavaScript way first so you can learn the concept. Then you can move on to using your favorite js frameworks like AngularJS, ReactJS and etc. </p>
<h3>Access-Control-Allow-Origin</h3>
<p>Keep in mind, most browsers do not allow accessing APIs from different domain names. A good way around that is using <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS">CORS</a> or if you're in a dev environment use a <a href="https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi">chrome extension</a> that will allow you access to a remote API. You can also use a free CORS proxy by putting <a href="https://crossorigin.me/">crossorigin.me/</a> in front of your remote API url and it will magically allow you to see the remote API without restriction. (don't use this on a production because the data is not secure) </p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="https://image.slidesharecdn.com/consumingrestin-net-101025201559-phpapp02/95/consuming-rest-in-net-6-638.jpg?cb=1422666566" alt="Consuming RESTful API example">
</div>
</div>
<!-- /END THE FEATURETTES -->
<hr class="featurette-divider">
<!-- FOOTER -->
<footer>
<p class="pull-right"><a href="#">Back to top</a></p>
<p>© 2017 VitWorks.com. · <a href="http://vitworks.com/contact-me/">Hire Me</a> </p>
</footer>
</div><!-- /.container -->
/* GLOBAL STYLES
-------------------------------------------------- */
/* Padding below the footer and lighter body text */
body {
padding-bottom: 40px;
color: #5a5a5a;
}
/* CUSTOMIZE THE NAVBAR
-------------------------------------------------- */
/* Special class on .container surrounding .navbar, used for positioning it into place. */
.navbar-wrapper {
position: absolute;
top: 0;
right: 0;
left: 0;
z-index: 20;
}
/* Flip around the padding for proper display in narrow viewports */
.navbar-wrapper > .container {
padding-right: 0;
padding-left: 0;
}
.navbar-wrapper .navbar {
padding-right: 15px;
padding-left: 15px;
}
.navbar-wrapper .navbar .container {
width: auto;
}
/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */
/* Carousel base class */
.carousel {
height: 500px;
margin-bottom: 60px;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
z-index: 10;
}
/* Declare heights because of positioning of img element */
.carousel .item {
height: 500px;
background-color: #777;
}
.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 500px;
}
/* MARKETING CONTENT
-------------------------------------------------- */
/* Center align the text within the three columns below the carousel */
.marketing .col-lg-4 {
margin-bottom: 20px;
text-align: center;
}
.marketing h2 {
font-weight: normal;
}
.marketing .col-lg-4 p {
margin-right: 10px;
margin-left: 10px;
}
/* Featurettes
------------------------- */
.featurette-divider {
margin: 80px 0; /* Space out the Bootstrap <hr> more */
}
/* Thin out the marketing headings */
.featurette-heading {
font-weight: 300;
line-height: 1;
letter-spacing: -1px;
}
/* RESPONSIVE CSS
-------------------------------------------------- */
@media (min-width: 768px) {
/* Navbar positioning foo */
.navbar-wrapper {
margin-top: 20px;
}
.navbar-wrapper .container {
padding-right: 15px;
padding-left: 15px;
}
.navbar-wrapper .navbar {
padding-right: 0;
padding-left: 0;
}
/* The navbar becomes detached from the top, so we round the corners */
.navbar-wrapper .navbar {
border-radius: 4px;
}
/* Bump up size of carousel content */
.carousel-caption p {
margin-bottom: 20px;
font-size: 21px;
line-height: 1.4;
}
.featurette-heading {
font-size: 50px;
}
}
@media (min-width: 992px) {
.featurette-heading {
margin-top: 120px;
}
}
var jsonURL = "/vitworks/pen/zNQPNV.js"; //saved JSON file for codePen demo only
// Get live data from darksky.net with your own API key
//https://api.darksky.net/forecast/your_own_api_key/38.9554942,-119.9453795?exclude=minutely,hourly
var ajax = new XMLHttpRequest();
ajax.onload = functionName;
ajax.onerror = errorFunctionName;
ajax.open("GET", jsonURL, true);
ajax.send();
function functionName() {
if (this.status == 200) { // request succeeded
var weather = JSON.parse(this.responseText);
// If Weather Alerts are available
if (weather.hasOwnProperty("alerts")){
// Create a carousel for each weather alert
for (var i = 0; i < weather.alerts.length; i++ ){
$(".carousel-indicators").append('<li data-target="#myCarousel" data-slide-to="'+ i +'"></li>');
$(".carousel-indicators li:first").addClass("active");
$(".carousel-inner").append('<div class="item"><div class="container"><div class="carousel-caption alert_'+ i +'"></div></div></div>');
$(".item:first").addClass("active");
$(".alert_"+ i ).append("<h1>" + weather.alerts[i].title +"</h1>");
//list all regions affected by this Alert
$(".alert_"+ i ).append("<h5></h5>");
var allRegions = []; // This could also be done with strings
for (var j = 0; j < weather.alerts[i].regions.length; j++){
allRegions.push(weather.alerts[i].regions[j]); //put all regions into array
}
$(".alert_"+ i + " h5" ).text(allRegions.join(", ")); //Put commas between regions
var alertIntro = shorten(weather.alerts[i].description, 310); // Shorten the alert message
$(".alert_"+ i ).append("<p>" + alertIntro + "</p>");
$(".alert_"+ i ).append('<p><a class="btn btn-lg btn-primary" href="'+ weather.alerts[i].uri +'" role="button">More Info</a></p>');
}; // End Alerts loop.
} else{
// There are no Alerts, create on carousel with a message
$(".carousel-inner").append('<div class="item active"><div class="container"><div class="carousel-caption"></div></div></div>');
$(".carousel-caption").append("<h1>No Weather Alerts</h1>");
$(".carousel-caption" ).append("<p>Currently there are no weather alerts in South Lake Tahoe area!</p>");
$(".carousel-caption" ).append('<p><a class="btn btn-lg btn-primary" href="http://www.weather.com" role="button">Weather Info</a></p>');
}
} else {
// handle more HTTP response codes here;
console.log(this.statusText);
}
}
function errorFunctionName(e) {
$(".carousel-inner").append('<div class="item active"><div class="container"><div class="carousel-caption"></div></div></div>');
$(".carousel-caption").append("<h1>No Weather Data Found</h1>");
$(".carousel-caption" ).append("<p>Can't load weather data. Check your internet connection.</p>");
$(".carousel-caption" ).append('<p><a class="btn btn-lg btn-primary" href="https://fast.com/" role="button">Check your internet connection</a></p>');
}
function shorten(text, maxLength) {
var ret = text;
if (ret.length > maxLength) {
ret = ret.substr(0,maxLength-3) + "...";
}
return ret;
}
Also see: Tab Triggers