AJAX调用即使在这个简单的表单上也不起作用。

时间:2022-04-26 01:25:39

Currently i have this code to test, because ajax calls for some reason i can't make it work, can anyone check what can be wrong?

目前我要测试这段代码,因为ajax出于某种原因调用它,我无法使它正常工作,谁能检查一下哪里出了问题吗?

test.php

test.php

<html>
<body>
 <form id="myform">
<input type="text" id="fname">
<input type="text" id="lname">
<input type="submit" id="data-send-button" value="Send Data">
</form>

<div id="responce-box"> </div>

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

    $("#myform").submit(function(e) {
        e.preventDefault();
        $.ajax({
            type : "POST",
            url : "submit.php",
            data : $("#myform").serialize(),
            beforeSend : function() {
                alert("indo");
            }

        });
        e.preventDefault();
    });

});
 </script>
  </body>
  </html>

submit.php

submit.php

<?php
echo "test";

That is just for testing. Thanks guys!

这只是为了测试。谢谢你们了!

2 个解决方案

#1


2  

As per your example I think you don't have include jquery

根据您的示例,我认为您没有包含jquery

try this:

试试这个:

<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<body>
 <form id="myform">
<input type="text" id="fname">
<input type="text" id="lname">
<input type="submit" id="data-send-button" value="Send Data">
</form>

<div id="responce-box"> </div>

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

    $("#myform").submit(function(e) {
        e.preventDefault();
        $.ajax({
            type : "POST",
            url : "submit.php",
            data : $("#myform").serialize(),
            beforeSend : function() {
                alert("indo");
            }

        });

    });

});
 </script>
  </body>
  </html>

#2


0  

your code is working fine but you need to include jquery library if you use jquery tag. so add this library and check your code it will be working and tested

代码运行良好,但如果使用jquery标记,则需要包含jquery库。因此,添加这个库并检查您的代码,它将工作和测试

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <html>
    <body>
    <form id="myform">
        <input type="text" id="fname">
        <input type="text" id="lname">
        <input type="submit" id="data-send-button" value="Send Data">
    </form>

    <div id="responce-box"></div>
    <script>
        $(document).ready(function () {

            $("#myform").submit(function (e) {
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "submit.php",
                    data: $("#myform").serialize(),
                    beforeSend: function () {
                        alert("indo");
                    }

                });
                e.preventDefault();
            });

        });
    </script>
    </body>
    </html>

submit.php

submit.php

<?php
echo "test";
?>

for more example

更多的例子

https://jquery.com/

https://jquery.com/

#1


2  

As per your example I think you don't have include jquery

根据您的示例,我认为您没有包含jquery

try this:

试试这个:

<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<body>
 <form id="myform">
<input type="text" id="fname">
<input type="text" id="lname">
<input type="submit" id="data-send-button" value="Send Data">
</form>

<div id="responce-box"> </div>

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

    $("#myform").submit(function(e) {
        e.preventDefault();
        $.ajax({
            type : "POST",
            url : "submit.php",
            data : $("#myform").serialize(),
            beforeSend : function() {
                alert("indo");
            }

        });

    });

});
 </script>
  </body>
  </html>

#2


0  

your code is working fine but you need to include jquery library if you use jquery tag. so add this library and check your code it will be working and tested

代码运行良好,但如果使用jquery标记,则需要包含jquery库。因此,添加这个库并检查您的代码,它将工作和测试

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <html>
    <body>
    <form id="myform">
        <input type="text" id="fname">
        <input type="text" id="lname">
        <input type="submit" id="data-send-button" value="Send Data">
    </form>

    <div id="responce-box"></div>
    <script>
        $(document).ready(function () {

            $("#myform").submit(function (e) {
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "submit.php",
                    data: $("#myform").serialize(),
                    beforeSend: function () {
                        alert("indo");
                    }

                });
                e.preventDefault();
            });

        });
    </script>
    </body>
    </html>

submit.php

submit.php

<?php
echo "test";
?>

for more example

更多的例子

https://jquery.com/

https://jquery.com/