jQuery ajaxSetup() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Ajax

Definition

jQuery.ajaxSetup(options) (since 1.1) sets default Ajax options merged into every future $.ajax(), $.get(), $.post(), and .load() call until the next setup. It returns a PlainObject of merged ajaxSettings.

Syntax

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

jQuery
$.ajaxSetup({ url: "ping.php" });

// url omitted → uses ping.php from setup
$.ajax({ data: { name: "Dan" } });

// Per-call options override setup
// Do NOT put success/error/complete in ajaxSetup
// Prefer $(document).on("ajaxSuccess", …) instead
CallWhat it does
optionsDefault key/value pairs (same as $.ajax())
Return valueMerged ajaxSettings PlainObject
Per-call optionsOverride matching setup keys for that request
Next $.ajaxSetup()Merges new keys into ajaxSettings

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

Parameters

ParameterDescription
Step 1Pass a PlainObject of the same keys as $.ajax() (url, type, dataType, global, …).
Step 2Later Ajax calls merge those defaults automatically.
Step 3Override any key on a single call by passing it explicitly.

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

Example 1 — Default url

Official idea: set a base URL once, then omit url on later calls.

jQuery
$.ajaxSetup({
  url: "https://jsonplaceholder.typicode.com/posts/1",
  dataType: "json"
});

$("#load").on("click", function () {
  // url not set here — uses ajaxSetup default
  $.ajax({
    data: { _: Date.now() }
  }).done(function (post) {
    $("#result").text("Loaded: " + post.title);
  });
});
Try It Yourself

How It Works

  1. Setup. $.ajaxSetup() stores default url and dataType.
  2. Click. The handler calls $.ajax() without a url.
  3. Merge. jQuery fills in the setup url for this request.

Example 2 — Default dataType: "json"

Shorthands like $.get() also inherit setup defaults.

jQuery
$.ajaxSetup({ dataType: "json" });

$("#load-user").on("click", function () {
  $.get("https://jsonplaceholder.typicode.com/users/1")
    .done(function (user) {
      $("#name").text(user.name);
    });
});
Try It Yourself

How It Works

  1. Default. Setup sets dataType: "json" globally.
  2. Shorthand. $.get() calls $.ajax() under the hood.
  3. Parse. jQuery parses the response as JSON automatically.

Example 3 — Per-Call Override

Explicit options on one request always beat setup defaults.

jQuery
$.ajaxSetup({
  url: "https://jsonplaceholder.typicode.com/users/1",
  dataType: "json"
});

$("#users").on("click", function () {
  $.ajax({}).done(function (user) {
    $("#log").text("Setup URL: " + user.name);
  });
});

$("#posts").on("click", function () {
  $.ajax({
    url: "https://jsonplaceholder.typicode.com/posts/1"
  }).done(function (post) {
    $("#log").text("Override URL: " + post.title);
  });
});
Try It Yourself

How It Works

  1. Setup. Default URL points at users.
  2. Inherit. Empty $.ajax({}) uses that URL.
  3. Override. The second call passes its own url.

Frequently Asked Questions

It sets default option values that jQuery merges into every future Ajax request — including $.ajax(), $.get(), $.post(), and .load() — until the next $.ajaxSetup() call. It returns a PlainObject of the merged ajaxSettings. Per-call options override matching defaults.
Global defaults affect every Ajax caller on the page, including plugins that expect jQuery’s normal defaults. A plugin’s internal $.get() might suddenly use your URL, method, or headers. Prefer explicit options per call or a small wrapper around $.ajax().
Yes. Any option you pass to $.ajax(), $.get(), $.post(), or .load() overrides the matching setup key for that call only.
$.ajaxSetup() sets static defaults merged into each request. $.ajaxPrefilter() runs per request after that merge and can change options dynamically, abort, or return a dataType. Use prefilters for conditional logic; avoid setup for new global config.
Putting success, error, or complete in $.ajaxSetup() attaches them to every Ajax call, including plugins. Bind global handlers with $(document).on("ajaxSuccess"), ajaxError, and ajaxComplete instead.
Call $.ajaxSetup() again with the keys you want to change — it merges into ajaxSettings. Prefer avoiding global setup so resets are rarely needed; use a wrapper helper instead.

Next: ajaxTransport() Method

Learn custom Ajax transports with send() and abort() when prefilters are not enough.

ajaxTransport() 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.

8 people found this page helpful