Home >Web Front-end >JS Tutorial >How to improve user experience with beforeSend
This time I will show you how beforeSend improves the user experience. What are the precautions for beforeSend to improve the user experience? The following is a practical case, let’s take a look.
jQuery is an open source js framework that is often used. There is a beforeSend method in the $.ajax request, which is used to execute some before sending the request to the server. action.
$.ajax({ beforeSend:function(){ // handle the beforeSend event }, complete:function(){ // handle the complete event } });
Prevent duplicate data
In actual project development, when submitting a form, often due to network or other reasons, the user clicks the submitbutton and mistakenly thinks If the operation is not successful, the submit button operation will be repeated. If the front-end code of the page does not do some corresponding processing, it will usually result in multiple pieces of the same data being inserted into the data library, resulting in an increase in dirty data. To avoid this phenomenon, disable the submit button in the beforeSend method in the $.ajax request, wait until the Ajax request is completed, and then restore the button's available state.
For example:$.ajax({ type:"post", data:studentInfo, contentType:"application/json", url:"/Home/Submit", beforeSend:function(){ //禁用按钮防止重复提交 $("#submit).attr({disabled:"disabled"}); }, success:function(data){ if(data=="Success"){ // 清空输入框 clearBox(); } }, complete:function(){ $("#submit").removeAttr("disabled"); }, error:function(data){ consloe.info("error:"+data.responseText); } });
Simulate Toast effect
When ajax requests the server to load the data list, it prompts loading("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); } });I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
jQuery+AJAX calling page background
Discussion and research on Ajax
The above is the detailed content of How to improve user experience with beforeSend. For more information, please follow other related articles on the PHP Chinese website!