Ajax displays message and hides right away

Asked

Viewed 53 times

0

This code of mine is a bit buggy, its function is to display the message and leave it there until it refreshes the page, but it is disappearing.

$('form#yes').on('click', function() {
    function isyes() {
        $.ajax({
            type: 'POST',
            dataType: 'JSON',
            url: 'vote/yes',
            data: $(this).serialize(),
            success: function(vote) {
                if (vote['computed'] === true) {
                    $('.text-success').show();
                    $('.text-success').html(vote['message']);
                } else if (vote['computed'] === false) {
                    $('.text-danger').show();
                    $('.text-danger').html(vote['message']);
                }
            }
        })
    }
    isyes();
})

Why? You have some Load and I’m not seeing?

  • Post your html code to help answer.

  • Try to put the function isyes outside the $('form#yes').on('click', function() {});

1 answer

1


I think it might be some kind of incompatibility with Function, since you are creating a Function within another, the most correct thing would be to just call Function in the click event:

function isyes() {
        $.ajax({
            type: 'POST',
            dataType: 'JSON',
            url: 'vote/yes',
            data: $(this).serialize(),
            success: function(vote) {
                if (vote['computed'] === true) {
                    $('.text-success').show();
                    $('.text-success').html(vote['message']);
                } else if (vote['computed'] === false) {
                    $('.text-danger').show();
                    $('.text-danger').html(vote['message']);
                }
            }
        });
    }

$('form#yes').on('click', function() {
    isyes();
})

Or leave only the ajax:

$('form#yes').on('click', function() {
        $.ajax({
            type: 'POST',
            dataType: 'JSON',
            url: 'vote/yes',
            data: $(this).serialize(),
            success: function(vote) {
                if (vote['computed'] === true) {
                    $('.text-success').show();
                    $('.text-success').html(vote['message']);
                } else if (vote['computed'] === false) {
                    $('.text-danger').show();
                    $('.text-danger').html(vote['message']);
                }
            }
        });
})

If this is not the problem check the console if by any chance this ajax is being called 2 times, because when using ajax to load, sometimes Function is cached in the current run causing the click to call the function 2 times (your code and the one that was cached) ai Uga everything, but I still recommend the withdrawal of this Function from inside the event click.

  • on the console shows nothing, but this is not the solution.

  • In the console it would show only if there was an error, but in the "network" tab it shows all the requests made, make sure you are not making 2 requests when clicking on the button. This may be making the condition TRUE in the 1st Request and the second Request FALSE in the system I’m developing happens a lot this.

  • The correct ajax would not give Reload, here is asking to confirm the return of the information in the form. What? I’m not getting it.

  • If this function is called when the "SUBMIT" button is clicked put inside the event click a " Return false; ", or remove the Submit button and put a simple buttom, otherwise it will run the Reload.

  • Haha face worth worked!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.