Ajax联系表问题

I am trying to put together a simple contact form with ajax, where users are not redirected to the contact.php file once the submission is done..

There is no errors.. I am always redirected..

Any idea please? Any suggestion is highly appreciated. Thanks!

contact.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="main.js"></script>

</head>
<body>

    <form action="contact.php" method="post" class="ajax">
        <div>
            <input type="text" name="name" placeholder="Your Name">
        </div>
        <div>
            <input type="email" name="email" placeholder="Your Email">
        </div>
        <div>
            <textarea name="message" placeholder="Your Message"></textarea>
        <div>
            <input type="submit" value="Send">
    </form>


</body>
</html>

contact.php

<?php 

if(isset($_POST['name'], $_POST['email'], $_POST['message'], $_POST['name'])) {
    print_r($_POST);
}


?>

main.js

$('form.ajax').on('submit', function() { 
        var that = $(this), 
        url = that.attr('action'), 
        type = that.attr('method'), 
        data = {}; 

    that.find('[name]').each(function(index, value) {
        var that = $(this),
        name = that.attr('name'),
        value = that.val();

        data[name] = value;

    });

    $.ajax({
        url: url,
        type: type,
        data: data,

        success:function(response) {
            console.log(response);
        }

    });

     return false;
});

You need to prevent the default form behavior.

$('form.ajax').on('submit', function(evt) { 
      evt.preventDefault();

You need to ouput json format so header need to be setting and you need to return json value to ajax success so need json_encode($object_or_array_form_php);

 <?php 

if(isset($_POST['name'], $_POST['email'], $_POST['message'], $_POST['name'])) {
   header("Content-type:application/json");
   $_POST['success'] = "You form is sending ...";
   echo json_encode($_POST); //this is the "response" param form ajax
}


?>

Hacked! Here's a little bit different way ..

<script>
        $(document).ready(function() {

            $('form').submit(function (event) {
                event.preventDefault();
                var name = $("#mail-name").val();
                var email = $("#mail-email").val();
                var message = $("#mail-message").val();
                var submit = $("#mail-submit").val();
                $(".form-message").load("contact.php", {
                    name: name,
                    email: email,
                    message: message,
                    submit: submit
                });
            });

        });
    </script>