$.ajaxSetup is used for ajax call in jquery. This works perfectly fine if we use jquery ajax.
But in MVC we use Ajax.BeginForm() .the callback handlers are OnFailure,OnSuccess,OnBegin.
Is there a way we can use $.ajaxSetup for Ajax.BeginForm().
Update
eg I have used Ajax.BeginForm() in 10 places but instead of writing OnFailure handler for all i just want to write a single common OnFailure in common place like jquery $.ajaxSetup. Is there a way of doing it?
You should look into using ajax options:
Defined:
@{
AjaxOptions ajaxOpts = new AjaxOptions
{
LoadingElementDuration = 2,
LoadingElementId = "someElementIdToShowLoading",
UpdateTargetId = "ElementIdToLoadTo"
};
}
Used:
@using (Ajax.BeginForm("ActionName", ajaxOpts)){}
See here for a full list: http://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxoptions_properties.aspx
Quote from the documentation of $.ajaxSetup
:
Note: Global callback functions should be set with their respective global Ajax event handler methods - .ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend() - rather than within the options object for $.ajaxSetup().
So as suggested, use the corresponding global AJAX event handlers such as .ajaxError()
for example if you want to handle all your AJAX errors globally:
$(document).ajaxError(function () {
console.log('oopsy');
});
Im reviewing the code and the library of microsoft for make the comunication with ajax is jquery.unobtusive-ajax.js and in that file they rewrited the main functionality for make a request, well only some options. But if they are using jquery why they need to modify that?? And of course with that code all the global hadlers are off.
I made a little modification is not cool but works, with more time i will update this with a cleaner solution.
Open file jquery.unobtusive-ajax.js.
Go to function asyncRequest and in the $.extend option you will see the four events beforeSend, complete, success and error you can raise the global triggers there.
Original code:
complete: function () {
loading.hide(duration);
getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(this, arguments);
},
Modified code:
complete: function () {
loading.hide(duration);
getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(this, arguments);
$(document).trigger('ajaxComplete');
},