美文网首页
jquery 中beforeSend 和 complete的使用

jquery 中beforeSend 和 complete的使用

作者: 青木川_ | 来源:发表于2019-03-30 13:30 被阅读0次

beforeSend:ajax请求触发。

1.用于在发送ajax请求之前设置表头。

beforeSend: function(request) {

    request.setRequestHeader("BBG-Key", "ab9ef204-3253-49d4-b229-3cc2383480a6");

}

2.防止数据重复:

当用户提交表单时候,虽然有时候已经点击了提交按钮,但有时候因为网络原因,会出现暂时没有返回数据的情况,用户会认为没有点击成功,从而造成了产生多条重复的数据——脏数据,所以我们可以在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请求服务器数据,提示loading (加载中,请稍后)。

Guid:全局唯一标识。

相关文章

网友评论

      本文标题:jquery 中beforeSend 和 complete的使用

      本文链接:https://www.haomeiwen.com/subject/jmbjbqtx.html