接收 66:285 未捕获的类型错误:无法读取未定义的属性“then”

问题描述

我设计了一个简单的ajax请求,用于从mvc中的数据库删除文件。为此,我使用 javascritp ajax 和 swal 提示进行删除。但它不起作用。我收到 .then 未定义错误

这是代码--

 $(".btnDel").click(function () {

    var NewFileName = $(this).val();
    var id =@Model.Pd.Id;
    console.log(id);
    console.log(NewFileName);

    swal({
        title: "Are you sure?",text: "You will not be able to recover this imaginary file!",icon: "warning",buttons: [
            'No,cancel it!','Yes,I am sure!'
        ],dangerMode: true,})

    .then(function (isConfirm) {
        if (isConfirm) {
             $.ajax({
                type: "POST",url: "@Url.Content("~/ManageProducts/DeleteExistingFile")/",data: { 'id': id,'fileName': NewFileName },success: function (data) {
                    swal("Message.",data,"success");
                    location.reload();

                },error: function () {
                    swal("","Something went wrong","error");
                }
             });
           
        }
        else
        {
         swal("Cancelled","Your imaginary file is safe :)","error");

        }
    });
});

enter image description here

enter image description here

解决方法

感谢您的编辑。下面的代码段定义了 idNewFileName 用于测试,并使用 example.com 进行 POST - 如果您尝试删除文件,则会收到“oops”消息。其他方面应该和帖子里的代码一样。

const id = "example_id";
const NewFileName = "example.pdf";
//**********************************

   swal({
        title: "Are you sure?",text: "You will not be able to recover this imaginary file!",icon: "warning",buttons: [
            'No,cancel it!','Yes,I am sure!'
        ],dangerMode: true,})

    .then(function (isConfirm) {
        if (isConfirm) {
             $.ajax({
                type: "POST",url: "https:www.example.com/delete",data: { 'id': id,'fileName': NewFileName },success: function (data) {
                    swal("Message.",data,"success");
                    location.reload();

                },error: function () {
                    swal("","Something went wrong","error");
                }
             });
           
        }
        else
        {
         swal("Cancelled","Your imaginary file is safe :)","error");

        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

我无法解释为什么该代码段有效,但在 VS 中运行它说 swal 返回 undefined。我会先在浏览器中测试代码,看看它是否仍然出错。

免责声明:这不是答案。 . .然而。如果有人解开谜团,我很乐意将其删除。