且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的Ajax表单提交两次

更新时间:2022-12-11 13:31:53

这是最有可能是您使用的是按钮提交来触发Ajax事件。试试这个:

  $('#exportForm)。递交(函数(五){
        即preventDefault();
        $阿贾克斯({
            键入:POST,
            网址:$(本).attr(行动),
            数据:$(本).serialize()
            成功:函数(响应){
                的console.log(响应);
            }
        });

        返回false;
    });
 

I've got a form that I'm capturing via jquery and sending via ajax. My problem is that the form submits more than once every time I refresh the page.

I've tried to unbind the submit button but then the form is posted normally after the first submit. Any help would be appreciated

$('#exportForm').submit(function(){

        $.ajax({
            type: "POST",
            url: $(this).attr( 'action' ),
            data: $(this).serialize(),
            success: function( response ) {
                $('#exportForm').unbind('submit');
                console.log( response );
            }
        });

        return false;
    });

It's most likely that you're using a button or submit to trigger the ajax event. Try this:

$('#exportForm').submit(function(e){
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: $(this).attr( 'action' ),
            data: $(this).serialize(),
            success: function( response ) {
                console.log( response );
            }
        });

        return false;
    });