Analysis on how to use Ajax beforeSend to improve user experience, ajaxbeforesend

Source: Internet
Author: User

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) { ("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(){"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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.