📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials jQuery in Practice AJAX with $.ajax

AJAX with $.ajax

6 min read Quiz at the end
$.ajax() makes HTTP requests with full control over method, headers, and callbacks. It returns a jqXHR Promise so you can chain .done(), .fail(), and .always() for clean async handling.

AJAX Requests

$.ajax({
  url: '/api/users',
  method: 'GET',
  dataType: 'json',
  success: function(data) {
    console.log(data);
  },
  error: function(xhr, status, err) {
    console.error(err);
  }
});
Topic Quiz · 5 questions

Test your understanding before moving on

1. Which jQuery method makes a GET request?
💡 $.get() performs an HTTP GET request and executes a callback on success.
2. What does $.ajax() return?
💡 $.ajax() returns a jqXHR object that implements the Promise interface.
3. Which option sets the HTTP method in $.ajax()?
💡 In $.ajax(), the 'type' option sets the HTTP method (GET, POST, etc.).
4. What does dataType: 'json' do?
💡 Setting dataType: 'json' tells jQuery to parse the response as JSON automatically.
5. Which callback runs regardless of success or failure?
💡 The .always() callback fires whether the request succeeds or fails.