WDI Day Thirty-Five: API’s and AJAX

Earlier this year I took a class in JavaScript development, so API’s and AJAX are somewhat familiar to me. Today I realized that I have a much better handle on these topics when it comes to tackling new projects, however, I’m looking forward to practicing my work with API’s so that I can feel more confident with that type of code.

Application Program Interface (API) is a service that provides raw data for the public. We reviewed how we can create our own APIs so that we can use them for CRUD functionality in apps, which was a pretty fun scenario to take a look at.

Asynchronous Javascript and XML (AJAX) do server side requests asynchronously on the client without having to send an actual browser request that reloads the page.

In class, we played around with the following code to make API requests – and I thought it was a pretty good chunk of code that I could use for future reference when it comes to API-related projects:

$(document).ready(function(){
$("h1").on("click", function(){
var url = "https://api.wunderground.com/api/api_key/geolookup/conditions/q/va/midlothian.json"
$.ajax({
url: url,
type: "get",
dataType: "json"
// $.ajax takes an object as an argument with at least three key-value pairs...
// (1) The URL endpoint for the JSON object.
// (2) Type of HTTP request.
// (3) Datatype. Usually JSON.
}).done(function(){
console.log("Ajax request success!")
}).fail(function(){
console.log("Ajax request fails!")
}).always(function(){
console.log("This always happens regardless of successful ajax request or not.")
})
})
})

Leave a Reply