【问题标题】:SweetAlert: How to send a POST request after clicking OK?SweetAlert:点击确定后如何发送 POST 请求?
【发布时间】:2014-11-20 09:02:07
【问题描述】:

我想要你的 - SweetAlert 我的网站。现在我需要使用此代码配置 SweetAlert,以便在单击“确定”按钮时,它将通过 POST formaction="/link/link" post="test=1"

swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: " warning ",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: " No, cancel plx!",
    closeOnConfirm: false,
    closeOnCancel: false
}, function(isConfirm) {
    if (isConfirm) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
    } else {
        swal("Cancelled", "Your imaginary file is safe: ) ", " error ");
    }
});

我想问你,我该如何构建它。

【问题讨论】:

  • 我假设您需要更改 if (isConfirm) {} 部分。您可以使用所需字段创建新的form,将其附加到body,然后使用form.submit()
  • 你能给我写个例子吗?
  • @bigbobr 他没说他有表格。
  • @Nabil Kadimi,在你的回答中,post() 函数完全符合我在下面写的;)
  • 你在这个JS中使用了ajax

标签: javascript html sweetalert


【解决方案1】:

在 swal 回调中添加 POST 请求触发器,如下所示:

swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: " warning ",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    cancelButtonText: " No, cancel plx!",
    closeOnConfirm: false,
    closeOnCancel: false
}, function(isConfirm) {
    if (isConfirm) {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
        post('/link/link', {test: '1'});  // <<< This is what I added
    } else {
        swal("Cancelled", "Your imaginary file is safe: ) ", " error ");
    }
});

The definition of the function post() is found here

【讨论】:

    【解决方案2】:

    我用 Promise 实现了 Preconfirm 方法...

    this.$swal({
                title: '¿Sure?',
                showCancelButton: true,
                confirmButtonText: 'Yes',
                cancelButtonText: 'No',
                showLoaderOnConfirm: true,
                preConfirm: function(result) {
                    return new Promise(function(resolve, reject) {
                        if (result) {
                            axios.post('url', {data:data})
                            .then(function(response){
                                toastr.success('Succes');
                                resolve();
                            })
                            .catch(function(error){
                                toastr.error('Error ');
                                console.log(error);
                                reject();
                            })
                        }
                    });
                },
                allowOutsideClick: () => !this.$swal.isLoading(),
            })
    

    【讨论】:

    • 这对我有用。但不确定函数“resolve()”和“reject()”应该做什么,所以我忽略了它们。
    • Resolve 方法,如果你使用它会直接返回到“then”方法(在这种情况下是 sweetalert);拒绝方法返回“false”并取消操作
    【解决方案3】:

    对于纯 JS - 这将发布参数 post 和值 post123

    var formTest = document.createElement('form');
    formTest.setAttribute("method", "post");
    formTest.setAttribute("action", "");
    
    var post = document.createElement("input");
    post.setAttribute("type", "hidden");
    post.setAttribute("name", "post");
    post.setAttribute("value", "post123");
    formTest.appendChild(post);
    
    document.body.appendChild(formTest);
    formTest.submit();
    

    如果你愿意,你可以使用需要 jQuery 的简短而漂亮的 AJAX 调用

    【讨论】:

      【解决方案4】:

      试试这个代码,根据你的需要编辑。

      swal({
       title: "Are you sure?",
       text: "You will not be able to recover this imaginary file!",
       type: " warning ",
       showCancelButton: true,
       confirmButtonColor: "#DD6B55",
       confirmButtonText: "Yes, delete it!",
       cancelButtonText: " No, cancel plx!",
       closeOnConfirm: false,
       closeOnCancel: false },
        function() {
          $.ajax({
            type: "POST",
            data: {
              'data_id': id
            },
            url: 'http://localhost/project/method',
            success: function(data) {
              swal("Deleted!", "Your imaginary file has been deleted.", "success");
            },
            error: function(data) {
              swal("Cancelled", "Your imaginary file is safe: ) ", " error ");
            }
          });
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-22
        • 1970-01-01
        • 2012-07-04
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多