There are two types of events that Ajax will trigger , one is a local event and the other is a global event:
Local events: Called and dispatched via $.ajax.
- $.ajax({
- beforeSend: function(){
- // Handle the beforeSend event
- },
- complete: function(){
- // Handle the complete event
- }
- // ...
- });
Global events can be bound with bind and unbind with unbind.
- $("#loading").bind("ajaxSend", function(){ //使用bind
- $(this).show();
- }).ajaxComplete(function(){ //Use ajaxComplete directly
- $(this).hide();
- });
However, sometimes you don't want to use global variables, at this time let global: false
- $.ajax({
- url: "test.html",
- global: false,
- // ...
- });
He also has some sequence of events:
The ajaxStart global event starts a new Ajax request and no other ajax requests are in progress at this time.
The beforeSend local event fires when an Ajax request starts, set the XHR object here.
ajaxSend global event The global event that fires before the request starts
success local event Fired when the request succeeds. When the server returns no errors, the returned data is also free of errors.
ajaxSuccess global event global request success
error local event Fired only when an error occurs. The two callback functions success and error cannot be executed at the same time.
ajaxError global event triggered when an error occurs globally
complete local event This event is fired when the request completes, regardless of whether the request succeeded or failed.
ajaxComplete global event Triggered when the global request is completed
ajaxStop global event Fired when no Ajax is in progress.