Home > Web Front-end > JS Tutorial > body text

How to improve user experience with beforeSend

php中世界最好的语言
Release: 2018-04-04 16:10:56
Original
1440 people have browsed it

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  
   }
});
Copy after login

Prevent duplicate data

In actual project development, when submitting a form, often due to network or other reasons, the user clicks the submit

button 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);
}
});
Copy after login

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);
}
});
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!