Stop jQuery Ajax POST from sending twice

  • 2 Replies
  • 2740 Views
Stop jQuery Ajax POST from sending twice
« on: May 03, 2016, 19:00:36 pm »
I have a simple html login form which I submit to my server via ajax POST request after the form is filled.

But every time I submit the form the POST is sent twice or too fast to notice in the network tab (in chrome tools).

What prompts this behavior ?

*

Ruhit

  • *
  • 22
Re: Stop jQuery Ajax POST from sending twice
« Reply #1 on: May 04, 2016, 16:21:34 pm »
If your form has a submit button and an Ajax POST call afterwards then the you might be sending POST request twice.

To stop this behavior you could use the event parameter (passed in as argument to form submit jQuery function) in order to prevent the submit of the form.

Code
$('#myForm').submit(function(event) {
    event.preventDefault();    // avoid to execute actual form submit.

    $.ajax({
    / ...
}

Re: Stop jQuery Ajax POST from sending twice
« Reply #2 on: May 04, 2016, 20:17:41 pm »
thanks for your input Ruhit

I tried using the submit event that you posted

I tried with the type="submit" on my <button> element and without

still not working for me :(

not sure what I did wrong

 

Sitemap 1 2 3 4 5 6 7 8