jQuery .ajax() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Ajax

Definition

jQuery.ajax() is the core Ajax method (since 1.0; URL+settings form since 1.5). Pass a settings object to send an HTTP request. It returns a jqXHR — chain .done(), .fail(), and .always(). $.get(), $.post(), and .load() are built on top of it.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.ajax({
  url: "/api/items",
  method: "GET",       // or type: "GET"
  dataType: "json"
}).done(function (data) {
  // success
}).fail(function () {
  // error
}).always(function () {
  // finished either way
});

// Also valid: $.ajax(url, settings)
CallWhat it does
urlEndpoint (*or set via $.ajaxSetup())
method / typeHTTP verb — default "GET"
dataQuery string (GET) or body (POST)
dataTypeHow to parse the response (json, html, …)
beforeSendLast chance to set headers or return false to cancel
globalDefault true — set false to skip global events

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Call $.ajax({ url, method, data, dataType, … }) (or $.ajax(url, settings)).
Step 2Chain .done() for success and .fail() for errors.
Step 3Use .always() for cleanup that must run either way.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Basic GET with .done()

Fetch JSON and read a field when the request succeeds.

jQuery
$("#load").on("click", function () {
  $.ajax({
    url: "https://jsonplaceholder.typicode.com/posts/1",
    method: "GET"
  }).done(function (data) {
    $(".log").text("Title: " + data.title);
  }).fail(function () {
    $(".log").text("Request failed.");
  });
});
Try It Yourself

How It Works

  1. Click. The handler calls $.ajax() with a URL and method.
  2. Send. jQuery starts an async GET and returns a jqXHR.
  3. Success. .done() receives the parsed response body.

Example 2 — POST with data

Send form-style fields in the request body and read the created resource.

jQuery
$("#post").on("click", function () {
  $.ajax({
    url: "https://jsonplaceholder.typicode.com/posts",
    method: "POST",
    data: { title: "Hello Ajax", body: "Posted with $.ajax()", userId: 1 }
  }).done(function (data) {
    $(".log").text("Created id: " + data.id + " — " + data.title);
  });
});
Try It Yourself

How It Works

  1. Method. method: "POST" puts data in the body (not the query string).
  2. Encode. By default jQuery sends application/x-www-form-urlencoded.
  3. Response. The demo API returns JSON with an id.

Example 3 — dataType + done / fail / always

Parse JSON automatically and chain all three promise-style handlers.

jQuery
$.ajax({
  url: "https://jsonplaceholder.typicode.com/posts/1",
  dataType: "json"
})
  .done(function (data) {
    $(".log").append("\ndone: userId=" + data.userId);
  })
  .fail(function () {
    $(".log").append("\nfail");
  })
  .always(function () {
    $(".log").append("\nalways (finished)");
  });
Try It Yourself

How It Works

  1. Parse. dataType: "json" turns the body into an object before .done().
  2. Success path. .done() runs only when the request succeeds.
  3. Error path. .fail() runs on HTTP or parse errors.

Frequently Asked Questions

jQuery.ajax() is the core jQuery method for HTTP requests. Pass a settings object (or a URL plus settings) to configure url, method, data, dataType, and more. It returns a jqXHR you can chain with .done(), .fail(), and .always(). Shorthands like $.get() and $.post() call $.ajax() internally.
A jqXHR object — a superset of XMLHttpRequest that also implements the Promise/Deferred interface. Chain .done(fn) for success, .fail(fn) for errors, and .always(fn) for either outcome. Call jqXHR.abort() to cancel an in-flight request.
Both run on success. success is a settings property: $.ajax({ success: fn }). done() is chained on the returned jqXHR: $.ajax(...).done(fn). Prefer done/fail/always — they compose with other Deferreds and match modern promise style.
When global is false, the request opts out of all global Ajax events on document — ajaxStart, ajaxSend, ajaxSuccess, ajaxError, ajaxComplete, and ajaxStop will not fire. Per-request done(), fail(), and always() still run.
Use $.get(url, data, success) for simple GET requests with minimal configuration. Use $.ajax() when you need full control: custom headers, beforeSend, POST with structured data, dataType, global: false, or multiple chained callbacks.
When dataType is "json", jQuery expects JSON and parses the body into a JavaScript value before .done() runs. If parsing fails, the request is treated as an error and .fail() runs. Other common values: "html", "text", "xml", "script".

Next: jQuery.ajaxPrefilter() Method

Inspect or rewrite Ajax options before a request is sent.

jQuery.ajaxPrefilter() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful