4
I usually see the use of the function of beforeSend:
of jQuery.ajax()
being used in a simple way, such as emptying a div before the request:
function enviar(){
$.ajax({
beforeSend: function(){
$("#div").empty();
},
...
});
}
Looking at the jQuery.ajax documentation(), It seems to me that this configuration has more useful depth than simply executing something so trivial before the request. That’s because, if it’s just to empty a div, for example, I could do it without the beforeSend:
, just put the $("#div").empty();
before AJAX:
function enviar(){
$("#div").empty(); // esvazia a div antes
$.ajax({
...
});
}
The documentation informs that the function of beforeSend:
can take two arguments:
Function( jqXHR jqXHR, Plainobject Settings )
When reading the descriptive text of the configuration, I could not understand. What would be the use and function of these arguments? How can they interfere with the request? If you can give an example the answer would be even better.
The idea is actually to modify the request before sending it by adding an authentication token, for example.
– Woss