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.
Remember
$.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";
Register once at startup, then every jQuery Ajax call can inherit the behavior.
Call $.ajaxPrefilter(handler) (optionally with a dataTypes string).
Inside the handler, read or mutate options; inspect originalOptions when you need the caller’s raw input.
Use jqXHR for patterns like abort-on-retry.
Optionally return "json" (or another dataType) to redirect processing.
Make requests with $.ajax(), $.get(), $.post(), or .load() as usual.
Piece
Required?
Role
dataTypes
Optional
Space-separated filter, e.g. "json script"
options
Yes (arg)
Merged settings — safe to mutate for this request
originalOptions
Yes (arg)
Caller input without ajaxSetup defaults
jqXHR
Yes (arg)
Request object — store / abort when needed
Pseudocode
Pseudocode
register ajaxPrefilter(optional dataTypes, handler)
when $.ajax() / $.get() / $.post() / .load() starts:
merge ajaxSetup defaults into options
for each matching prefilter (by dataTypes, if any):
call handler(options, originalOptions, jqXHR)
if handler returns a dataType string:
set request dataType to that value
run dataType-specific prefilters too
run beforeSend (if set)
send via transport
Reference
Cheat Sheet
Keep these facts handy while wiring global Ajax hooks.
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.
Opt in. Only requests that set the flag are affected.
Example 3 — Scope by dataTypes: "json"
Run the handler only for JSON requests — HTML or script calls skip it.
Click #fetch → Authorization header sent
Public demo API ignores the token
.done() still shows the user email
How It Works
Token. Keep the secret outside the click handler.
Merge. Extend options.headers so existing headers are kept.
Send. Every later Ajax call gets the Bearer header automatically.
Alt. Wrap options.beforeSend if you need the raw XHR or cancel with false.
Edge Cases & Pitfalls
Check these before shipping global Ajax prefilters.
scope
Affects every matching Ajax call
Including third-party plugins that use jQuery Ajax. Prefer dataTypes filters or custom flags so you do not surprise unrelated traffic.
options
options vs originalOptions
Mutate options for the request. originalOptions is the pristine caller input — do not treat them as the same object.
order
Runs after ajaxSetup, before beforeSend
Defaults from $.ajaxSetup() are already merged. beforeSend still runs later and can cancel with false.
return
Returning a dataType redirects
return "script" changes transport and runs script-specific prefilters. Only return a string when you mean to redirect.
fetch
Does not wrap fetch() or axios
Prefilters only see jQuery Ajax ($.ajax and shorthands). Native fetch() never enters them.
Compatibility
Browser Support
jQuery.ajaxPrefilter() has been part of jQuery since 1.5. It works in every browser that runs your loaded jQuery version — no separate browser feature flag.
✓ Stable · jQuery 1.5+
jQuery.ajaxPrefilter()
Supported across jQuery 1.5+, 2.x, and 3.x. Prefer a current jQuery 3.x build for modern projects. Requires jQuery Ajax — fetch() and axios do not use prefilters.
100%With jQuery Ajax
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
ajaxPrefilterUniversal
Bottom line: Safe for everyday apps. Register once, filter by dataTypes when possible, and keep handlers light.
Analysis
Performance Notes
Approach
Cost
Best for
One light prefilter
Cheap
Headers, flags, small option tweaks
dataTypes-scoped prefilter
Cheap
JSON-only or script-only logic
Heavy work in every prefilter
Expensive
Avoid — keep handlers tiny
Prefilters run on every matching request. Keep them synchronous and small — no DOM thrashing or large JSON parsing inside the handler.
Remember
Key Takeaways
Role: global hook before each jQuery Ajax request is sent.
Choose: prefilter for logic; ajaxSetup for static defaults; beforeSend for last-moment XHR.
One line:$.ajaxPrefilter() lets you change Ajax options (or abort / redirect) before every jQuery request is sent.
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.