Callback function for completion of ajax request

now there is a requirement to show the load component before the request and hide the load display immediately after the request is completed. But the Ajax value provides four callback functions: beforeSend,success,error,complete. To hide the load shows the component, either in success and error to add hidden load logic, or in the complete function to add once, but in this way if an exception occurred in the success function, you will not go to the complete function, it will not be able to hide the loading component, is there any way to write only once hidden load logic?

 $.ajax({
    url: url,
    data: this.reqSearch,
    complete:function(XMLHttpRequest,textStatus){
        that.$Spin.hide();
    },
    success: function(res) {
    ......
    },
    error: function(){
    ......
    }
 })
May.31,2021

.then (). Catch () ?

then is forced to report an error. How sick.
both then and fail go through one method?

$.ajax({
    urL:'/',
    beforeSend: v=>console.log('beforeSend'),
    error: v=>console.log('error'),
    dataFilter: v=>console.log('dataFilter'),
    success: v=>console.log('success'),
    complete: v=>console.log('complete')
})
.then(v=>console.log('then'))
.fail(v=>console.log('fail'))

beforeSend is called before the request is sent, passing in a XMLHttpRequest as an argument.
error is called when there is an error in the request. Pass in a XMLHttpRequest object, a string describing the error type, and an exception object, if any,
dataFilter is called after the request succeeds. Pass in the returned data and the value of the "dataType" parameter. And the new data (which may have been processed) must be returned and passed to the success callback function.
success is called after the request. Pass in the returned data and a string containing the success code.
complete this function is called when the request is completed, regardless of success or failure. Pass in a XMLHttpRequest object and a string containing a success or error code.


$.ajax provides these callbacks. Your requirement complete should be the most appropriate. In other words, an exception will occur in success . Shouldn't you do debugging to avoid it? The best solution to this brain circuit is to put success and error processing codes in complete , first that.$Spin.hide (); and then according to Status .

Menu