简体   繁体   English

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

[英]Ajax not reaching 'success' for login form

The success part of the ajax function is never reached. 永远不会达到ajax函数的成功部分。 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:

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

HTML: 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. 在beforeSend中,不会生成带有xml的警报框。 the other one does so I know it's at least calling the function. 另一个这样做,所以我知道至少要调用该函数。

You need to cancel the form submission from happening 您需要取消表单提交

change 更改

onclick="validLogin()"/>

to

onclick="validLogin(); return false;"/>

it would be better to use onsubmit on the form and not a click event on the button. 最好在表单上使用onsubmit ,而不要在按钮上使用click事件。 It would be even better to not use inline event handlers. 最好不使用内联事件处理程序。

Spokey solved this in chat. Spokey在聊天中解决了这个问题。 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? jQuery Ajax-状态码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. 首先,正如espascarello指出的那样,表单没有返回假,因此它正在重新发送数据。 With that amended the url was not found which was odd so using an absolute path solve it. 经过修改后,未找到奇怪的网址,因此使用绝对路径即可解决该网址。

Thank you everyone for your help 谢谢你们每一个人的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM