SweetAlert:点击确定后如何发送POST请求?

时间:2014-11-20 09:02:07

标签: javascript html sweetalert

我想要你 - 我的网站SweetAlert。现在我需要使用这段代码配置SweetAlert,这样就可以了解" OK"按钮,它将通过POST格式发送=" / link / link"交="测试= 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 ");
    }
});

我想问你,我怎样才能把它建成。

4 个答案:

答案 0 :(得分:3)

在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

答案 1 :(得分:1)

对于纯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调用

答案 2 :(得分:1)

我用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(),
        })

答案 3 :(得分:0)

尝试此代码,只需根据需要进行编辑。

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 ");
      }
    });
  }