An analysis of skillfully using AJAX Beforesend to improve user experience _ajax related

Source: Internet
Author: User

jquery is a frequently used open source JS framework in which the $.AJAX request has a Beforesend method that performs some action before sending a request to the server.

$.ajax ({
   beforesend:function () {
   //Handle the Beforesend event   
   },
   complete:function () {
// Handle the Complete event  
   }
});

Prevent duplicate data

In the actual project development, when submitting a form often because of the network or its reasons, the user clicks the Submit button mistakenly believe that he did not operate successfully, and then repeated the operation of the button, if the page front-end code does not do some corresponding processing, will often lead to many of the same data into the database, resulting in increased dirty data. To avoid this, disable the Submit button in the Beforesend method in the $.ajax request, and wait until the AJAX request is finished, in the available state of the Restore button.

As an example:

$.ajax ({
type: "Post",
Data:studentinfo,
contentType: "Application/json",
URL: "/home/submit" ,
beforesend:function () {
   //disable button prevents duplicate commit
   $ ("#submit). attr ({disabled:" disabled "});
Success:function (data) {
   if (data== "Success") {
     //Empty input box
     clearbox ();
   }
,
complete: function () {
   $ ("#submit"). Removeattr ("Disabled");
},
error:function (data) {
   Consloe.info (" Error: "+data.responsetext);
}"
;

Simulate toast effect

Ajax requests the server to load the list of data when prompted loading ("in load, please later ...")

$.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 skillfully with Ajax beforesend to improve the user experience is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.