jquery is an open-source JS framework that is often used, where the $.AJAX request has a Beforesend method that performs some action before sending a request to the server.
For details, refer to the official jquery document: http://api.jquery.com/Ajax_Events/
$. Ajax ({beforesend function ({< Span class= "CO" >//Handle the Beforesend event}, Complete: function< Span class= "Hljs-function" > () { //Handle the Complete Event} //... )
Prevent duplicate data
In the actual project development, the submission of the form is often due to the network or its reasons, the user click the Submit button mistakenly believe that they did not succeed, and then repeat the button operation times, if the page front-end code does not do some corresponding processing, often lead to many of the same data inserted into the database, resulting in increased dirty data. To avoid this behavior, disable the Commit button in the Beforesend method in the $.ajax request and wait until the AJAX request is complete and the recovery button is available.
As an example:
Submit form data to background processing$.Ajax{Type:"POST",Data: Studentinfo,ContentType:"Application/json",Url:"/home/submit",Beforesend:function(){Disable buttons to prevent duplicate submissions$("#submit").attr{Disabled:"Disabled"});},Success:function(Data{if (data=="Success"){Empty the input boxClearbox ();}},Complete:function ({< Span class= "at" >$ ( Removeattr ( "disabled") ;}, error: Function (data) {console. info ( "error:" + Data. responsetext) }})
Simulate toast effects
The AJAX request server loads the data list when prompted loading ("load, please later ..."),
$.Ajax{Type:"POST",ContentType:"Application/json",Url:"/home/getlist",Beforesend:function(){$("Loading").Show ();},Success:function(Data{if (data=="Success"){// ...}},Complete:function ( Span class= "Hljs-params") {$ ( " Loading "). hide () ;},error: function ( Span class= "Hljs-params" >data) {console. info ( "error:" + data. ResponseText) ;} )
Using AJAX Beforesend to improve user experience