SweetAlert不等待

时间:2018-12-17 21:36:19

标签: javascript jquery

我在ajax中使用swal,但是swal不要等到您发出请求后,这是我的代码:

$("#btnSubmit").click(function() {
    var data = $("#myForm").serialize();

    $.ajax({
        type: "POST",
        url: myURL,
        data: data,
        success: function(response) {
            swal({
                title: "Success",
                text: "Data added.",
                icon: "success"
            })
                .then(() => {
                    window.location.replace(referrer);
                });

        }
    });
});

1 个答案:

答案 0 :(得分:0)

我还没有运气重现您的问题。有关示例,请参见代码段。控制台中是否有任何错误?这些对我们来说很有帮助。

const btn = document.querySelector('#test');

btn.addEventListener('click', () => {

  fetchTodos()
    .then(() => {
      swal({
        title: 'Success',
        text: "This is in fetchTodos().then()",
        icon: 'success',
      }).then(() => console.log('Do something here'));
    })

});

function fetchTodos() {
  return fetch('https://jsonplaceholder.typicode.com/todos/1')
    .then(response => response.json())
    .then(json => console.log(json))
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
<button id="test">
  SweetAlert
</button>