Ajax At Your Service
When learning AJAX first showed up on my calendar, this was all I could think of:
Lil’ bit of info:
- Asynchronous: Two or more events are existing or happening at the same time. For example, when click on a button, like adding a head of lettuce to your shopping cart, this can happen independently of your other actions.
- XML/JSON: Data transferred in the request
One example of an Ajax request is when you’re rating a show on Netflix in your profile. You can move quickly from flick to film to TV show, tossing out ratings in bulk, instead of rating one show and refreshing each time, which would slow you down, and you would have never found all the seasons of SVU which is in of itself, a modern day tragedy.
Anyway, Let’s take a look at an example of an Ajax request pulling from the City of Chicago’s data.
var landmarksDiv = document.getElementById('landmarks');
On our first two lines we’re creating a function to pull out the Landmarks of Chicago by their unique ID’s. The good stuff is the next part.
“$” defines that we are using jQuery. Next, is our “get” request which we can also use “post” for, this selector finds our HTML elements. Next we’re calling the API, and pulling out that JSON to populate our page.
And we did all of that without refreshing.