AJAX Form Post不起作用


AJAX Form Post not working

我至少尝试了3种方法,每次我尝试使用ajax发布表单时,页面总是重新加载,但没有提交任何内容。

它甚至没有返回成功或错误——似乎函数甚至没有被调用。

这是代码,提前谢谢。

HTML:

<form id='sig_up' name='sig_up' style='min-width:170px'>
    <textarea id='sig' class='custom-scroll' style='max-height:180px;'></textarea>
    <br>
    <input class='btn btn-primary btn-sm' type='submit' /> 
</form>

jQuery/AJAX:

    $('#sig_up').on('submit',function(e) {
        $.ajax({
        url:'update_sig.php',
        data:$(this).serialize(),
        type:'POST',
        success:function(data){
            $.smallBox({
            title : "Signature Updated",
            content : "Your signature has been successfully updated.",
            color : "#296191",
            //timeout: 8000,
            icon : "fa fa-bell swing animated"
        });
        },
        error:function(data){

        }
        });
        e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
});

好吧,要发送它,我会将表单的处理程序附加到文档中(尽管根据文档,这并不重要),首先编写preventDefault,以避免在任何事情之前发送表单(尽管我在最后尝试了它,结果完全一样,但必须查看此函数)。

所以,我做的另一件重要的事情,我认为是真正的答案,是表单内部范围的改变。如果你在AJAX内部使用$(this),你指的是AJAX jquery处理程序,而不是表单,我改变了它来显示它是如何改变的。我还添加了一个错误控制台.log来检查哪里出了问题,以防万一。我尝试了下面的代码,它在我的本地主机中发送了调用。

  $(document).on('submit', '#sig_up', function(e) {
        e.preventDefault();
        var $form = $(this);
        $.ajax({
            url:  'update_sig.php',
            data: $form.serialize(),
            type: 'POST',
            success:function(data){
                console.log('ok');
//              $.smallBox({
//                  title : "Signature Updated",
//                  content : "Your signature has been successfully updated.",
//                  color : "#296191",
//                  //timeout: 8000,
//                  icon : "fa fa-bell swing animated"
//              });
            },
            error:function(data){
                console.log(data);
            }
        });
});

您用stopImmediatePropagation()尝试过吗?

   $('#sig_up').on('submit',function(e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        $.ajax({
            url:   'update_sig.php',
            data:  $(this).serialize(),
            type:  'POST',
            success:function(data){
              console.log("Success");
            },
            error:function(data){
              console.error("Error");
            }
        });
    });
    $('#sig_up').on('submit',function(e) {
         e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
         var that = this;
             $.ajax({
             url:'update_sig.php',
             data:$(that).serialize(),
             type:'POST',
             success:function(data){
                 $.smallBox({
                    title : "Signature Updated",
                    content : "Your signature has been successfully updated.",
                    color : "#296191",
                    //timeout: 8000,
                    icon : "fa fa-bell swing animated"
                });
            },
            error:function(data){

           }
        });
    });