微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决接收 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。我会先在浏览器中测试代码,看看它是否仍然出错。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。