jQuery.ajaxPrefilter([dataTypes], handler) (since jQuery 1.5) registers a hook that runs before each Ajax request is sent and before options are fully processed. Use it to tweak settings, handle custom flags, or redirect a request to another dataType.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
// mutate options before the request is sent
});
// Optional: only for matching dataTypes
$.ajaxPrefilter("json script", function (options) { /* ... */ });
// Return a dataType string to redirect processing
// return "script";
Call
What it does
dataTypes
Space-separated filter, e.g. "json script"
options
Merged settings — safe to mutate for this request
originalOptions
Caller input without ajaxSetup defaults
jqXHR
Request object — store / abort when needed
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Call $.ajaxPrefilter(handler) (optionally with a dataTypes string).
Step 2
Inside the handler, read or mutate options; inspect originalOptions when you need the caller’s raw input.
Step 3
Use jqXHR for patterns like abort-on-retry.
Return Value
Return Value
Description
Value / undefined
Getters usually return a value from the first matched element, or undefined when nothing applies.
jQuery Examples
Example 1 — Basic Registration
Log the URL and add a custom header before every Ajax request.
User types quickly in #search
Each keystroke starts $.ajax with abortOnRetry: true
Stale jqXHR for same URL is aborted
Only the latest .done() updates #count
How It Works
Custom flag.abortOnRetry is ignored by the transport — your prefilter reads it.
Store. Keep the current jqXHR keyed by options.url.
Abort. A new call to the same URL aborts the previous one.
Example 3 — Scope by dataTypes: "json"
Run the handler only for JSON requests — HTML or script calls skip it.
JSON button → prefilter logs + user name
HTML button → prefilter skipped → different message
How It Works
Filter. The first argument limits the handler to dataType: "json".
Match. The user request runs the prefilter and sets Accept.
Skip. The HTML request never enters this handler.
Frequently Asked Questions
jQuery.ajaxPrefilter() registers a handler that runs before each Ajax request is sent and before jQuery finishes processing the settings. Use it to modify options, handle custom flags (like abortOnRetry), scope by dataTypes, or return a dataType string. Every $.ajax(), $.get(), $.post(), and .load() call passes through registered prefilters.
options is the merged settings object jQuery will use — including defaults from $.ajaxSetup() and ajaxSettings. originalOptions is exactly what you passed to $.ajax() (or what a shorthand built), without those merged defaults. Compare them when you need to know whether a value came from the caller or from global setup.
When you call $.ajaxPrefilter("json script", handler), the handler runs only for matching dataTypes. Omit dataTypes to run on every Ajax request. Space-separate multiple types in one string.
If your prefilter returns a string such as "script" or "json", jQuery treats the request as that dataType. That affects transport selection and runs prefilters registered for that dataType.
$.ajaxSetup() sets static default values merged into every request. $.ajaxPrefilter() runs a function per request with access to options, originalOptions, and jqXHR — better for conditional logic, aborting duplicates, or rewriting URLs. Prefilters run after ajaxSetup merge.
Prefilters run earlier, before beforeSend. They can change options for the whole pipeline and optionally return a dataType. beforeSend runs once per request just before send, receives the XHR, and can return false to cancel. Use prefilters for cross-cutting option changes; use beforeSend for last-moment XHR tweaks or cancel.