In jquery ajax transmission request is complete and the uses of beforesend

BeforeSend main users are the following methods:

Request for setting a header before sending the request ajax

  I.e. as the front, if we want to set before transmitting data request header can be done as follows:

beforeSend: function(request) {
     request.setRequestHeader("BBG-Key", "ab9ef204-3253-49d4-b229-3cc2383480a6");
}, 

Prevent data duplication

  When the user submits the form, although sometimes has been clicked the submit button, but due to network reasons, there will be a temporary situation did not return data, users will think success does not click, it will cause the database to produce a number of duplicate data --- dirty data, so we can add beforeSend disabled submit button function, after complete recovery, the following:

// submit the form data to the spooler 
$ .ajax ({ 
    of the type: "POST" , 
    the Data: studentInfo, 
    contentType: "the Application / json" , 
    url: "/ Home / the Submit" , 
    beforeSend: function () {
         // disable button Submit prevent duplicate 
        $ ( "# Submit") attr ({Disabled: "Disabled." }); 
    }, 
    Success: function (Data) {
         IF (Data == "Success" ) {
             // clear input box 
            ClearBox (); 
        } 
    }, 
    Complete:function () {
        $("#submit").removeAttr("disabled");
    },
    error: function (data) {
        console.info("error: " + data.responseText);
    }
});

Analog toast effect

  When prompted loading ajax request to the server to load data list ( "Loading, please wait ...")

$.ajax({
    type: "post",
    contentType: "application/json",
    url: "/Home/GetList",
    beforeSend: function () {
        $("loading").show();
    },
    success: function (data) {
        if (data == "Success") {
            // ...
        }
    },
    complete: function () {
        $("loading").hide();
    },
    error: function (data) {
        console.info("error: " + data.responseText);
    }
});

Guess you like

Origin www.cnblogs.com/webmc/p/11403734.html