Why does the AJAX submit form trigger a page refresh?

Why does the page refresh when < H1 > AJAX submits the form? < / H1 >
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>TTT</title>
</head>
<body>
    <form id="f" method="get">
        <input type="text" placeholder="A">
        <button type="submit">B</button>
    </form>
    <script>
    document.getElementById("f").onsubmit = function (e) {
        console.log(e);
        const xhr = new XMLHttpRequest();
        xhr.open("get", "", true);
        xhr.send();
    };
    </script>
</body>
</html>

the short code above, click the button, and the submission event is expected to be printed in console , and then the page is not refreshed. As a result, TM refreshes the whole page every time you click the button-

. < H2 > method attempted < / H2 >

add return false to the end of the onsubmit function to submit asynchronously without refreshing the page.

but

however, I"m pretty sure I"ve never used return false in my previous asynchronous commit. I don"t know why it suddenly didn"t work this time. Is there a big god who can answer it?

attach the code that is still successful without return false before-

function post_tab() {
        // 
        document.getElementById("work_form").onsubmit = function (e) {
            e.preventDefault();
            const xhr = new XMLHttpRequest(),
                f = e.target,
                formData = new FormData(f),
                log = $("-sharpprint_log");
            xhr.open("POST", f.action, true);
            xhr.responseType = "json";
            xhr.onreadystatechange = function () {
                if(xhr.readyState === 4 && xhr.status === 200){
                    const data = xhr.response;
                    console.log(data);
                    print_tab(data["qrs"], log[0]);
                    log.scrollTop(log[0].scrollHeight - log.height());
                    input_clean("input_qr");
                }
            };
            xhr.send(formData);
        }
    }

this function post_tab use onclick to bind to the submit button-

<button class="btn btn-success form-control" onclick="post_tab()"></button>

Why does this code still submit asynchronously without refreshing the page every time return false is added?

Aug.22,2021

e.preventDefault (); and return false; are equivalent
to block default events.


if you don't stop the form default event, of course you refresh the page


the problem should lie in < button type= "submit" > B < / button >. Button has been changed, and another type type has been changed

.
Menu