Ajax beforeSend和complete 方法
阿新 • • 發佈:2019-01-30
$.ajax({
beforeSend: function(){
// Handle the beforeSend event
},
complete: function(){
// Handle the complete event
}
// ......
});
其中的$.ajax請求中有一個beforeSend方法,用於在向伺服器傳送請求前執行一些動作。
complete 方法執行完後呼叫
用途
防止重複資料
// 提交表單資料到後臺處理
$.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) {
console.info("error: " + data.responseText);
}
});
模擬Toast效果
$.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);
}
});