使用ajax发布表单,然后将相同的表单提交到其他url


Post form with ajax and then submit same form to other url

为什么我的表单在ajax提交后没有再次提交到另一个URL。

使用下面的脚本,我想用ajax发布表单,然后将相同的表单提交到其他url。

首先,我的表单将提交到Ajax URL(http://www.mywebsite.com/insertdata.php)然后相同的表单将提交到表单操作URL(http://www.example.com/mail.php)

HTML

<form id="contact_form" action="http://www.example.com/mail.php" method="post" novalidate="novalidate" data-track="formSubmit" name="contact_form">
 <!------Form Elements ------->
<input class="form-control submit" type="submit" value="Submit">
</form>

JavaScript

$("form").on("submit", function(event) {
        event.preventDefault();
        $.ajax({
            url: "http://www.mywebsite.com/insertdata.php",
            type: "post",
            crossDomain: true,
            data: $(this).serialize(),
            success: function(responce) {
                console.log(responce);
                return true;
            }
        });
    });

删除event.preventDefault();
如果调用此方法,则不会触发事件的默认操作。这将停止表单提交到action属性中的url。

$("form").on("submit", function(event) {
              event.preventDefault();
              flag=false;
                $.ajax({
                    url: "http://www.mywebsite.com/insertdata.php",
                    type: "post",
                    crossDomain: true,
                    data: $(this).serialize(),
                    success: function(responce) {
                        console.log(responce);
                        return true;
                        $("form").off("submit");
                        $("form").trigger( "submit" )
                    }
                });      
        });
   $("#contact_form").on("submit", function(event) {
        $.ajax({
            url: "http://www.mywebsite.com/insertdata.php",
            type: "post",
            crossDomain: true,
            data: $(this).serialize(),
            success: function(response) {
                console.log(response);
                return true;
            }
        });
    });

在成功函数中添加一个表单提交。

$("form").on("submit", function(event) {
    event.preventDefault();
    $.ajax({
        url: "http://www.mywebsite.com/insertdata.php",
        type: "post",
        crossDomain: true,
        data: $(this).serialize(),
        success: function(responce) {
            console.log(responce);
            $("form").submit();
            return true;
        }
    });
});

更新日期:你可以使用按钮点击,它将工作

$(".submit").on("click", function(event) {