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

Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.