【发布时间】:2019-01-02 13:26:11
【问题描述】:
提交表单时,我需要运行 addclass 并显示加载动画,然后运行 ajax 但是在 ajax 中添加 false 到 async 时,首先运行 ajax,然后加载 anamiatin 执行。
$('.form-login').submit(function(e) {
$('#alert-massages').html("").removeClass("alert alert-warning");
$(".btn-login").addClass('loading');
$(".btn-login").attr('disabled','disabled');
if(formLogin.valid()) {
$.ajax({
url : "<%=checkOtpLoginUrl %>",
dataType : "json",
async: false,
data : {
"<%=renderResponse.getNamespace() %>msisdn" : $("#<%= renderResponse.getNamespace() %>msisdn").val(),
"<%=renderResponse.getNamespace() %>password" : $("#<%= renderResponse.getNamespace() %>password").val(),
"<%=renderResponse.getNamespace() %>captchaText" : $("#<%= renderResponse.getNamespace() %>captchaText").val(),
"<%=renderResponse.getNamespace() %>rememberMe" : $("#<%= renderResponse.getNamespace() %>remember").val()},
method : "post"
}).done(function(data) {
if (data.result) {
【问题讨论】:
-
首先,不要使用
async: false。曾经。如果最近的浏览器更新甚至不支持它,我不会感到惊讶。更正后,问题是否仍然存在? -
我需要使用异步:false
-
这不是问题。这是预期的行为。添加
async:false(你应该永远这样做)强制你的代码是同步的,这意味着只要ajax调用没有完成,一切都会冻结(这就是你永远不应该使用它的原因),因此加载动画直到最后才会出现。为什么你需要使用async:false?什么原因?
标签: javascript jquery ajax