Rails beginner
Rails beginner

Reputation: 14514

Jquery help repeated ajax call

When I submit my form, a repeated ajax call is made it never stop.

Here is my Jquery for the ajax call on form submit:

$('form#formgo').submit(function(e) {
    e.preventDefault();

    $('#comparecontent').empty().html(
        '<p style="text-align:center;">' +
        '<img src="../images/ajax.gif" /></p>');

    var form = $(this).closest('form');

    $.ajax({
        type: form.attr('method'),
        url: form.attr('action'),
        data: form.serialize(),
        success: function(msg){
            $('#comparecontent').html(msg);
        }
    });

    return false;
});

Upvotes: 0

Views: 659

Answers (2)

ace
ace

Reputation: 7603

It seems that something is triggering submit.
Try event.stopPropagation()

$('form#formgo').submit(function(e) {
    e.preventDefault();
    e.stopPropagation();
    ...

Also check if an onchange event is bound to elements with id comparecontent.

Upvotes: 0

SteamDev
SteamDev

Reputation: 477

In your ajax call, try specifying a timeout for the request:

$.ajax({
        type: form.attr('method'),
        url: form.attr('action'),
        data: form.serialize(),
        timeout: 3000, // time in milliseconds
        success:function(msg){
$('#comparecontent').html(msg);
    }
});

see the .ajax() documentation for more info on available params http://api.jquery.com/jQuery.ajax/

Upvotes: 3

Related Questions