jQuery ajaxTransport() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Ajax

Definition

jQuery.ajaxTransport(dataType, handler) (since 1.5) registers a factory that can return a transport object with send and abort. jQuery uses that object to transmit the request for the given dataType.

Syntax

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

jQuery
$.ajaxTransport(dataType, function (options, originalOptions, jqXHR) {
  if (/* can handle */) {
    return {
      send: function (headers, completeCallback) { /* transmit */ },
      abort: function () { /* cancel */ }
    };
  }
  // return undefined → jQuery tries another transport
});

// Pipeline: ajaxSetup → ajaxPrefilter → transport.send → completeCallback
CallWhat it does
dataTypeWhich requests ask this factory first
factoryfunction(options, originalOptions, jqXHR)
sendfunction(headers, completeCallback)
abortCancel in-flight work; ignore late callbacks

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

Parameters

ParameterDescription
Step 1Call $.ajaxTransport("yourType", factory) once at bootstrap.
Step 2Inside the factory, inspect options (and optionally originalOptions, jqXHR).
Step 3If you can handle it, return { send, abort }; otherwise return nothing.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Factory Skeleton

Return a transport only when you can handle the request; otherwise fall through.

jQuery
$.ajaxTransport("demo", function (options) {
  if (options.type === "GET" && options.url.indexOf("demo://") === 0) {
    return {
      send: function (headers, completeCallback) {
        var payload = options.url.replace("demo://", "");
        completeCallback(200, "success", {
          demo: "Demo says: " + payload
        });
      },
      abort: function () {}
    };
  }
});

$("#run").on("click", function () {
  $.ajax({
    url: "demo://Hello transport",
    dataType: "demo"
  }).done(function (text) {
    $("#log").text(text);
  });
});
Try It Yourself

How It Works

  1. Register. The factory is tied to dataType: "demo".
  2. Gate. Only demo:// GET URLs return a transport.
  3. Respond. responses.demo matches the dataType key.

Example 2 — Official Image Transport Pattern

Load images with new Image() instead of XHR when dataType: "image".

jQuery
$.ajaxTransport("image", function (s) {
  if (s.type === "GET" && s.async) {
    var image;
    return {
      send: function (_, callback) {
        image = new Image();
        function done(status) {
          if (!image) return;
          var statusText = status === 200 ? "success" : "error";
          var tmp = image;
          image = image.onerror = image.onload = null;
          callback(status, statusText, { image: tmp });
        }
        image.onload = function () { done(200); };
        image.onerror = function () { done(404); };
        image.src = s.url;
      },
      abort: function () {
        if (image) {
          image = image.onerror = image.onload = null;
        }
      }
    };
  }
});

$("#load").on("click", function () {
  $.ajax({
    url: "https://picsum.photos/200",
    dataType: "image"
  }).done(function (img) {
    $("#preview").empty().append(img);
  });
});
Try It Yourself

How It Works

  1. Gate. Only async GET requests use this transport.
  2. Load. Setting image.src starts the browser fetch.
  3. Complete. Success/error call callback with an image response key.

Example 3 — Mock JSON Without HTTP

Return hard-coded JSON instantly for mockjson:// URLs — great for prototypes.

jQuery
$.ajaxTransport("mockjson", function (options) {
  if (options.url.indexOf("mockjson://") === 0) {
    return {
      send: function (headers, completeCallback) {
        var id = options.url.replace("mockjson://user/", "");
        var user = {
          id: parseInt(id, 10) || 1,
          name: "Mock User " + id,
          email: "user" + id + "@example.test"
        };
        completeCallback(200, "success", { mockjson: user });
      },
      abort: function () {}
    };
  }
});

$("#fetch").on("click", function () {
  $.ajax({
    url: "mockjson://user/42",
    dataType: "mockjson"
  }).done(function (user) {
    $("#out").text(user.name + " — " + user.email);
  });
});
Try It Yourself

How It Works

  1. Match. Only mockjson:// URLs return a transport.
  2. Build. send creates a fake user from the URL id.
  3. Key. responses.mockjson matches dataType: "mockjson".

Frequently Asked Questions

jQuery.ajaxTransport(dataType, handler) registers a factory that jQuery calls when selecting how to transmit a request for that dataType. If the factory can handle the call, it returns an object with send and abort. Otherwise it returns undefined and jQuery tries another transport. This is the lowest-level Ajax extension point.
send(headers, completeCallback) starts transmission and must eventually call completeCallback when finished. abort() cancels an in-flight request — clear timers, detach Image handlers, or ignore late callbacks so completeCallback does not fire after cancel.
On jQuery 3.7 (common in tutorials), use the positional form: completeCallback(status, statusText, responses, headers). status is like 200 or 404; statusText is "success" or "error"; responses maps dataType keys to values (for example { image: imgElement }); headers is an optional response-header string. Newer jQuery versions also accept a single params object.
ajaxPrefilter runs earlier to tweak options (url, headers, cache) or abort. ajaxTransport replaces how bytes are sent and received for matching dataTypes. Prefer prefilters for option changes; use transports only when prefilters and converters are not enough.
As a last resort: custom protocols, Image-based loads, in-memory mocks, or non-HTTP sources. For ordinary JSON, HTML, or script requests, built-in $.ajax() transports already work — do not register a custom transport.
Each request needs its own transport instance with isolated state (timers, Image nodes, sockets). Registering one shared object would let concurrent requests clobber each other. The factory returns a fresh { send, abort } per matching request.

Next: jQuery.get() Method

Learn the everyday GET shorthand that uses jQuery’s built-in transports.

jQuery.get() 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.

6 people found this page helpful