duanpu2272 2014-04-08 18:22
浏览 29
已采纳

Ajax POST首次尝试失败

I have a wierd issue with the below code:

<script type="text/javascript">
    $(document).ready(function(){
        $('#loginSubmit').click(function () {
            //start the ajax
            var f = $("#loginForm");
            request = $.ajax({
                //this is the php file that processes the data
                url: "app/index.php",
                type: "POST",
                data: f.serialize(),
                dataType:"json",
                cache: false
            });
            request.done(function (response, textStatus, jqXHR){
                // log a message to the console
                console.log("Hooray, it worked!");
                // callback handler that will be called on failure
                request.fail(function (jqXHR, textStatus, errorThrown){
                    // log the error to the console
                    console.error(
                        "The following error occured: "+
                        textStatus, errorThrown
                    );
                    alert("fail")
                });
                // callback handler that will be called regardless
                // if the request failed or succeeded
                request.always(function () {
                    // reenable the inputs
                    alert("ok")
                });
            });
        });
    });
</script>

I am using firebug to debug the code and basically when the submit button is clicked, the post request receives no response, however if I then resend the post data using firebug it all works fine?

Any ideas?

UPDATE - FORM CODE

<form id="loginForm" action="" method="post" name="loginForm">
    <h1>Login Form</h1>
    <div>
        <input type="text" placeholder="Email" required="" id="email" name="userEmail" />
    </div>
    <div>
        <input type="password" placeholder="Password" required="" id="userPassword" name="userPassword" />
    </div>
    <div>
        <input type="hidden" value="login" id="tag" name="tag" />
        <input type="submit" value="Log in" id="loginSubmit" name="loginSubmit" />
        <a href="#">Lost your password?</a>
    </div>
</form>
  • 写回答

2条回答 默认 最新

  • dtry54612 2014-04-08 18:27
    关注

    change your button type for preventing default submission of FORM

    submit to button


    Or change your event of click to submit

    $('#loginform').submit(function(event){
        event.preventDefault();
    
        $.ajax({
            //this is the php file that processes the data
            url: $(this).attr("action"),
            type: "POST",
            data: $(this).serialize(),
            dataType:"json",
            cache: false
        })
            .done(function (response, textStatus, jqXHR){
                // log a message to the console
                console.log("Hooray, it worked!");
            })
            .fail(function (jqXHR, textStatus, errorThrown){
                // log the error to the console
                console.error(
                    "The following error occured: " + textStatus, 
                    errorThrown
                );
                alert("fail");
            })
            .always(function () {
                // callback handler that will be called regardless
                // if the request failed or succeeded
                // reenable the inputs
                alert("ok");
            });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀