I have something like this
$.ajaxSetup({
cache: false
});
I am wonder can you do ajaxSetup like you can do $.ajax
var jqxhr = $.ajax({ url: "example.php" })
.success(function() { alert("success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
So could I do
var a = $.ajaxSetup({
cache: false
})
then
a.success(function() { alert("success"); })
I have not had a chance to try to this out yet. Once I have few minutes I going to try this but if someone knows that would be good too. I also don’t see anything in the documentation.
AFAIK, no you won’t be able to do that –
.ajaxSetup()isn’t a defered object, unlike.ajax()as of jQuery v1.5. So you will just get an error saying the object doesn’t have the methods.If what you are trying to do is setup things to happen with every
.ajax()call (.ajaxSetup()sets up defaults for all.ajax()calls). You can access global ajax callbacks like:$.ajaxComplete(function(){alert("complete");})$.ajaxError(function(){alert("error");})$.ajaxSuccess(function(){alert("success");})if you want to perform actions on ALL ajax calls if that is what you are aiming for? You will also have access to all the callback parameters:
.ajaxError(event, XMLHttpRequest, ajaxOptions, thrownError)Updates to follow up comments:
As per the docs for .ajaxSetup():
So it looks like there is no global handler for the statusCode like the callback added in 1.5. However again as per the docs:
So I would think, you could use for example:
Take a look at the demo here it shows a successful and an un-successful
.ajax()call – showing local and global actions, including dealing with different statusCodes.