Analysis on how to use Ajax beforeSend to improve user experience, ajaxbeforesend
JQuery is a frequently used open-source js framework. The $. ajax request has a beforeSend method, which is used to execute some actions before sending a request to the server.
$.ajax({ beforeSend:function(){ // handle the beforeSend event }, complete:function(){// handle the complete event }});
Prevent duplicate data
In actual project development, when submitting a form, users often click the submit button due to network or other reasons and mistakenly believe that the operation is not successful. In this case, the button operation times are repeated, if the front-end code of the page is not processed, multiple identical data records are usually inserted into the database, resulting in an increase in dirty data. To avoid this problem, disable the submit button in the beforeSend method of the $. ajax request. Wait until the Ajax request is executed and the restore button is available.
For example:
$. Ajax ({type: "post", data: studentInfo, contentType: "application/json", url: "/Home/Submit", beforeSend: function () {// disable the button to prevent repeated submission $ ("# submit ). attr ({disabled: "disabled"}) ;}, success: function (data) {if (data = "Success ") {// clear the input box clearBox () ;}}, complete: function () {$ ("# submit "). removeAttr ("disabled") ;}, error: function (data) {consloe.info ("error:" + data. responseText );}});
Toast Simulation
The ajax request server prompts loading when loading the data list ("loading, please wait ...")
$.ajax({type:"post",contentType:"application/json",url:"/Home/GetList",beforeSend: function(){ $("loading").show();},success: function(data){ if (data=="Success"){ // ... }},error: function(){ console.info("error:"+data.responseText);}});
The above analysis uses Ajax beforeSend to improve user experience, which is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's home.