AJAX - Get Data with $.get()

Published on: July 22, 2018

Welcome to the 13th Easy jQuery Tutorial, part of EasyProgramming.net. I covered AJAX in the Easy JavaScript series. jQuery makes much of that easier. AJAX is short for Asynchronous JavaScript And XML. Today, we'll look at how to use the $.get() Ajax method in jQuery.

I would recommend checking out the AJAX videos I did in the Easy JavaScript series. I explain in-depth what AJAX is and how to write it in vanilla JavaScript.

Here's a quick example of what a $.get() method implementation looks like. 

    $.get(url,data, successCallbacK, dataType)
  	.done(function(){})
	.fail(function(){})
	.always(function(){});

The url is the url that you are getting information from. 

The data is what you are sending in your request to the server. It can be one thing or multiple items in JSON format. It is optional.

The successCallback function is a method that you can use to capture returned data and do something with it. It is also optional.

The dataType is the type of data you are looking to receive. The most common is JSON. But you can request text, html, or xml as well. It's optional so you can let the script figure out the kind of data it's receiving. 

As you can see, you can chain special methods to your get call such as done, fail, and always. They are kind of self explanatory but here's just to confirm what you already figured out:

  • done - triggers if the get call completed successfully
  • fail - triggers if the call fails for some reason - you can capture the error and display it in whatever form of error handling you do
  • always - triggers whether or not the ajax completes or fail

Practice the $.get() method! There are others that you can find in future tutorials. 

I hope you enjoyed this tutorial! Please feel free to ask questions below or just leave your comments. 

To fork the fiddle and follow along: https://jsfiddle.net/9gyr5knj/

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!

Resources:



Comments: