【问题标题】:How do I wait for an action to finish before showing the next alert using sweetalert2如何在使用 sweetalert2 显示下一个警报之前等待操作完成
【发布时间】:2021-01-10 19:34:44
【问题描述】:

所以我有一个 ASP.NET MVC Core 项目,当我单击一个按钮时,我想使用 sweetalert2 显示一个警报。

按钮看起来像这样

<a class="btn btn-primary" onclick="doFunction(@(JsonConvert.SerializeObject(number))); ">Click</a>

JavaScript 看起来像这样

function doFunction(model) {
        Swal.queue([{
            title: 'TestData',
            confirmButtonText: 'OK',
            text: 'TestData',
            showLoaderOnConfirm: true,
            preConfirm: () => {
                $.ajax({
                        type: "POST",
                        url: '@Url.Action("Tester", "Home")',
                        contentType: "application/json; charset=utf-8",
                        data: { data: "yourdata" },
                        dataType: "json",
                        //success: function(recData) { alert('Success'); },
                        //error: function() { alert('A error'); }
                }).then(Swal.insertQueueStep("Woo", "Woo"))
                    .catch(() => {
                        Swal.insertQueueStep({
                            icon: 'error',
                            title: 'Something went wrong'
                        })
                    });
            }
        }])
    }

这是行动

public IActionResult Tester()
        {

            using (var context = new DbContext(_context))
            {
                var user = context.Users.FirstOrDefault(x => x.UserName == "TheUser");
                return new JsonResult(user.UserName);
            }
        }

现在它显示一个警报,当我单击“确定”时,它会显示下一个警报,但中间没有加载窗口。

如何正确调用控制器内部的操作并在等待操作完成时显示加载警报,然后显示完成警报。

【问题讨论】:

标签: javascript asp.net asp.net-mvc sweetalert sweetalert2


【解决方案1】:

有一个DONE, Fail option in ajax, move it under there..

$.ajax({
   url: "test.html",
   context: document.body
 }).done(function() {
  $( this ).addClass( "done" );
 });

   $.ajax({  
        type: "POST",
         url: '@Url.Action("Tester", "Home")',
         contentType: "application/json; charset=utf-8",
         data: { data: "yourdata" },
         // dataType: "json", // comment this out

    // blah blah... and then the done option... below
    
    done: function(data){ 
        if (doSomeValidcheck(data)){
                Swal.insertQueueStep("Woo", "Woo"))
                // blah blah ...
            return true;
        } else {
            return false;
        }
    }
});

看来我们需要删除datatype json


更新:

将完成移动到 Ajax 调用的 结束 像这样...

$.ajax({
    type: "POST", 
    url : postUrl,
    data: data
}).done(function()  {
    alert("Success.");
})

【讨论】:

  • 嗯,好像 done 函数永远不会触发
猜你喜欢
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 2016-09-24
  • 1970-01-01
相关资源
最近更新 更多