M
M
Masterstvo2016-08-16 14:03:37
JavaScript
Masterstvo, 2016-08-16 14:03:37

AJAX duplicates sending data to the server. How to be?

Please tell me who knows what could be the matter. There is a script that sends data to the server using AJAX. On this server, the data is processed and entered into the database, but for some reason they are duplicated (that is, they are entered into the database twice). Without AJAX (with page reload) everything is fine! The script is the following:

$(document).ready(function() {
        $('#form').submit(function (e) {
            e.preventDefault();
            $.ajax({
                url: '/',
                type: 'POST',
                data: {text: $('#message-text').val()},
            });
        });
    });

Maybe something to add, so as not to be duplicated. Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Leo Developer, 2016-08-16
@Masterstvo

If none of the above help, insert

.......
e.preventDefault()
e.stopImmediatePropagation()
........
or you can insert it right away =)
Perhaps you have several events triggered on one element.

M
Mikhail Osher, 2016-08-16
@miraage

Look at the network tab.
If the request left alone - the problem is in the backend, we are looking for it there.
If there are several requests, we write debugger;and look for an asshole.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question