dpjo15650 2013-07-19 13:24
浏览 48
已采纳

Ajax没有达到登录表单的“成功”

The success part of the ajax function is never reached. I've spent hours on it, still no success. The code has been stripped of virtually everything necessary just to try and remove any possible problems but it still doesn't work.

Below is the stripped out code that I'm trying to get to work. Thanks for any help.

Ajax:

function validLogin(){

    var url = "loginProcessAjax.php";

    $.ajax({
        type: 'POST',
        url: url,

        beforeSend: function() {
            //
            alert("sdfs");
        },

        success: function(html) {

            alert("success");
            alert(html.getElementsByTagName("worked")[0].firstChild.nodeValue);
        },

        error: function( error ) {
            console.log(error);
        }
    });
}

PHP:

 <?php
 header("Content-type: text/xml");
 echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
 echo "<worked>";
 echo "success";
 echo "</worked>";
 ?>

HTML:

<form method="post" action="" id="ourLoginFormID_JS">
                    <div class="ourContactFormElement2">
                        <label for="username">Username:</label>
                        <input type="text" id="username"  name="username" autocomplete="off" class="required" value="<?php if(isset($_POST['username'])) echo htmlentities($_POST['username']); ?>"  />
                    </div>

                    <div class="ourContactFormElement2">
                        <label for="password">Password:</label>
                        <input type="password" id="password" name="password" autocomplete="off" class="required"/>
                    </div>

                    <div class="ourContactFormElement2">
                        <label> &nbsp; </label>
                        <input type="submit" name="loginButton" id="loginButton" value="Login!" onclick="validLogin()"/>
                    </div>

                    <div id="statusLogin"></div>
                </form>

In beforeSend, the alert box with xml doesn't get generated. the other one does so I know it's at least calling the function.

  • 写回答

2条回答 默认 最新

  • doukang1962 2013-07-19 14:19
    关注

    Spokey solved this in chat. Legend.

    by using $.ajax({
        type: 'POST',
        url: 'loginProcessAjax.php',
        beforeSend: function() {
            alert("sent");
        },
        success: function(html) {
            alert(html) 
        },
        error: function( x, status, error ) {
            alert(x.status + status + error);
        }
    });
    

    we were able to get an error code which then led to

    jQuery Ajax - Status Code 0?

    which then in turn led to a couple of things. Firstly as espascarello pointed out the form was not returning false so it was resending the data. With that amended the url was not found which was odd so using an absolute path solve it.

    Thank you everyone for your help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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