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?
A. $.post()
B. $.get()
C. $.fetch()
D. $.request()
💡 $.get() performs an HTTP GET request and executes a callback on success.
2. What does $.ajax() return?
A. undefined
B. A jQuery object
C. A jqXHR (deferred) object
D. A Promise
💡 $.ajax() returns a jqXHR object that implements the Promise interface.
3. Which option sets the HTTP method in $.ajax()?
A. type
B. method
C. verb
D. action
💡 In $.ajax(), the 'type' option sets the HTTP method (GET, POST, etc.).
4. What does dataType: 'json' do?
A. Sends JSON
B. Converts response to JS object
C. Requires JSON input
D. Creates JSON file
💡 Setting dataType: 'json' tells jQuery to parse the response as JSON automatically.
5. Which callback runs regardless of success or failure?
A. always()
B. finally()
C. complete()
D. done()
💡 The .always() callback fires whether the request succeeds or fails.
Submit answers