在 ajaxStop 之前执行的表单提交处理程序

Form submit handler executing before ajaxStop

我已将整个项目的一些处理程序设置为 show/hide 加载 div。

其中一些是 ajaxStartajaxStop(都绑定到 $(document))和绑定到 formssubmit

我不明白为什么 ajaxStop 在下面示例中的 form.submit 之后执行:

var blobs = [];

var echo = function echo(data, type) {
  var blob = window.URL.createObjectURL(new Blob([data], {
    "type": type
  }));

  blobs.push(blob);

  return blob
};

$(document).ready(function() {
  $('form').on('submit', function(e) {
    console.log('form submit');
    e.preventDefault();
  });
}).ajaxStart(function() {
  console.log('ajaxStart');
}).ajaxStop(function() {
  console.log('ajaxStop');
});

$('button').click(function() {
  $.get(echo("Sample data", "text/html"), function(data) {
    if (data != '')
      $('#frm').submit();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<form id="frm">
  <button type="button">
    Send
  </button>
</form>

原因是因为 ajaxStop()complete 事件触发所有活动的 AJAX 请求后被调用。

您在控制台中看到 form submit 是因为您在 success 处理程序中触发了该事件,该事件在 complete 之前发生。