rovsen
rovsen

Reputation: 5022

ajax post triggering twice

In an asp.net mvc3 application there is a form. Form is posted via jquery post. Below is the related code.

                $(this).find('form').live('submit', function () {
                    var request = $.post($(this).attr('action'), $(this).serialize(), function (data) {
                       //do something    
                    });
                    request.fail(function (jqXHR, textStatus) {
                       //do something    
                    });
                    return false;
                });

live() is used because form is loaded via ajax and I didn't want to bind click event each time form is retrieved via ajax.

The problem is clicking submit causes form to be posted twice. Tested in chrome and IE9, occurs in both of them.

So what else may cause this issue?

Upvotes: 0

Views: 1832

Answers (3)

De Shan Baptiste
De Shan Baptiste

Reputation: 288

This is what did it for me. I had an Ajax form and the only thing I got to work was:

$('#docForm').ajaxForm({

  ...Do Some Mildly Cool Stuff Code...

}).submit(function (e) {
  return false;
}); //Just add the submit to the end.

And that was all it took.

Hope it helps!

Upvotes: 0

Alexandros B
Alexandros B

Reputation: 1891

I suspect you are bining the form, or the submit button twice. There is a bookmarklet called Visual Event (not mine). Run in a check if this is the case. As kaz said without the actual html not much can be done

Upvotes: 1

kaz
kaz

Reputation: 1943

it's hard to tell without html markup, maybe you have two nested forms?

Try with event.stopPropagation or preventDefault:

$(this).find('form').live('submit', function (e) {
        e.stopPropagation();
        e.preventDefault();
        var request = $.post($(this).attr('action'), $(this).serialize(), function (data) {
                    });
        request.fail(function (jqXHR, textStatus) {
                       //do something    
        });
        return false;
});

Upvotes: 0

Related Questions