是什么在我的 ajax 代码中创建无限循环

What is creating the infinite loop in my ajax code?

本文关键字:创建 无限循环 代码 ajax 我的 是什么      更新时间:2023-09-26

这应该很容易,我只是在将近一个小时内无法抓住问题所在,所以也许我错过了一些东西。

我想创建一个没有"谢谢"页面的响应式联系表单,所以我正在使用 ajax。

我写了所有的代码,这是我在控制台上得到的回复:

Uncaught RangeError: Maximum call stack size exceeded

它一直无限运行。

这是我的表格:

    <div id="contactform" style="position: relative; top: 180px; left: 50px;">
        <table width="600px" class="contactform" >
            <tr>
                <td colspan="2">  Full Name: <br/> </td>
                <td colspan="2"> <input type="text" id="fullname" />   </td>    
            </tr>

            <tr>
                <td colspan="2">  Phone Number: </td>
                <td colspan="2"> <input type="text" id="telephone"/> </td>
            </tr>

            <tr>
                <td colspan="2">  Email Adress: </td>
                <td colspan="2"> <input type="text" id="email"/> </td>
            </tr>

            <tr>
                <td colspan="2"> Subject: </td>
                <td colspan="2"> <input type="text" id="subject" /> </td>
            </tr>
            <tr>
                <td colspan="2"> Content: </td>
                <td colspan="2"> <textarea rows="5" cols="50" id="text"> </textarea> </td>
            </tr>
            <tr>
                <td colspan="2">  </td>
                <td colspan="2"> <input type="button" class="link" value="Send" id="sendBtn" /> </td>
            </tr>
        </table>
    </div>      

这是我在.js文件中的 Ajax 代码部分:

$('#sendBtn').click(function(){
    //get info 
    console.log("dude!");
    var fullname = $("#fullname").val();
    var telephone = $("#telephone").val();
    var email = $("#email").val();
    var subject = $("#subject").val();
    var text = $("#text").val();
    //send info to php 
    $.ajax({
        beforeSend: function() {
    //      $("#spin").html(spiner);
        },
        url: 'http://www.example.com/MYtest/contact.php', 
        type: "POST", 
        data: ({ "fullname": fullname, "telephone": telephone, "email": email, "subject": subject, "text": text }), 
        success: function (results){
    //      $("#spin").hide(spiner);
            console.log(email);
            //hide table 
            $('#contactform').hide('slow', function() {
                // Use arguments.callee so we don't need a named function
                $('#contactform').hide( "slow", arguments.callee );
              });
            //show textboxes
            $('#aboutSuccess').show("slow");
        }
    }); 

});

这是我的内容.php :

    if(isset($_POST['email'])) {
        $email_to = "meEmail@example.com";
        $email_subject = "You have a new email from example.com";
        $fullname = $_POST['fullname'];
        $telephone = $_POST['telephone']; 
        $email = $_POST['email'];       
        $subject = $_POST['subject'];   
        $text = $_POST['text'];     
        $message = "Full Name: ". $fullname ."'n Telephone: ". $telephone ."'n Email: ". $email ."'n Subject: ". $subject ."'n 'n Message: 'n". $text;
        mail($email_to, $email_subject, $message);
    }

你在函数中调用arguments.callee。这就是导致循环的原因。

$('#contactform').hide('slow', function() {
    // Use arguments.callee so we don't need a named function
    $('#contactform').hide( "slow", arguments.callee );
});

我不明白你想用这段代码做什么,也许是剩菜。尝试将其替换为:

 $('#contactform').hide('slow');