Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created August 17, 2012 12:25
Show Gist options
  • Save cowboy/3378441 to your computer and use it in GitHub Desktop.
Save cowboy/3378441 to your computer and use it in GitHub Desktop.
jQuery: How can I create a catch-all Ajax transport?
// When I set up a json-specific transport...
$.ajaxTransport("json", function(options) {
console.log("json", options);
});
// The json options are logged.
$.getJSON("/foo");
// When I try to set up a catch-all transport...
$.ajaxTransport("*", function(options) {
console.log("catch-all", options);
});
// Nothing is logged. The transport factory function doesn't execute.
$.ajax("/foo");
@jaubourg
Copy link

You want to place your transport in front of all the existing ones, or else, a transport will be selected before you got a chance to give yourse. Hence the use of + in front of the dataType selector.

$.ajaxTransport( "+*", function(options) {
  console.log("catch-all", options);
});

Same goes for prefilters, even if order is a bit less important there.

@cowboy
Copy link
Author

cowboy commented Aug 17, 2012

Thanks! I must have missed the + in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment