jQuery AJAX

Load data with $.get, $.getJSON and $.ajax.

✍️ 3 exercises + code challenge in this lesson

jQuery simplifies AJAX (loading data without reloading the page):

  • $.get(url, callback)
  • $.getJSON(url, callback)
  • $.post(url, data, callback)
  • $.ajax({ url, method, data, success, error }): full control
  • $("#box").load("page.html"): load HTML into an element

Key Takeaways: jQuery AJAX

Mastering jQuery AJAX helps you write clean and reliable jQuery code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

jQuery AJAX Code Example

jQuery
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<button id="load">Load posts</button>
<ul id="posts"></ul>

<script>
  $("#load").click(function () {
    $("#posts").html("<li>Loading...</li>");
    $.getJSON("https://jsonplaceholder.typicode.com/posts?_limit=4")
      .done(function (posts) {
        $("#posts").empty();
        $.each(posts, function (i, post) {
          $("#posts").append($("<li>").text(post.title));
        });
      })
      .fail(function () {
        $("#posts").html("<li>Could not load posts.</li>");
      });
  });
</script>
Free sign-in required · Google login available

jQuery AJAX Practice Exercises

Check what you learned in “jQuery AJAX”.

/ 3 solved
Fill in the blank

Load JSON from a URL.

$.("/api/posts", function (data) { });
✓ Correct! Not quite. Try again.
Multiple choice

What does AJAX let you do?

✓ Correct! Not quite. Try again.
Code challenge

Load https://jsonplaceholder.typicode.com/users/1 with $.getJSON and put the user's name in #name.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services