0

我有这个 HTML

<section id="login">
    <form>
        <input type="text" name="username" size="10" placeholder="username" />
        <input type="password" name="password" size="10" placeholder="password" />
        <input type="submit" name="login" value="Login" />
    </form>
</section>

有了这个$.ajax()调用PHP(sys_login.php)。

$(function(login){
$('#login form').submit(function(e){

    var uname   =   $('input[name="username"]').val();
    var pwd     =   $('input[name="password"]').val();

    var dataObject = {
            command     : "login",
            username    : uname,
            password    : pwd
        };

    $.ajax({
        type     : "POST",
        url      : "application/sys/sys_login.php",
        data     : dataObject,
        dataType : "json",
        success: function(response, status){
            if (response.status===true){
                docCookies.setItem("sessionid", response.sessionid);
                docCookies.setItem("username", response.username);
                docCookies.setItem("roles", response.roles);
                location.href = "application/index.php";

            }else{
                console.log("false")
                }           
            },
        beforeSend:function(){
            console.log("sending")
        }
    }).error(function() {
        console.log("error");
    });
    e.preventDefault();
});
});

但它从未进入sys_login.php文件。如果我使用GET,它将到达 php 文件。在调试期间,即使控制台也没有打印任何内容。我做错了什么?

* *UPDATE 显然后端服务器出现故障。该脚本适用于其他机器。谢谢你们的时间。

4

3 回答 3

2

您应该stringify(dataObject)在将其发送到 PHP 之前。

尝试

    $.ajax({
        type     : "POST",
        url      : "application/sys/sys_login.php",
        data     : JSON.stringify(dataObject),
        dataType : "json",
)};

看这里

于 2013-11-13T06:27:44.257 回答
0

试试这个代码:

 $(function(login){
    $('#login form').submit(function(e){

        var uname   =   $('input[name="username"]').val();
        var pwd     =   $('input[name="password"]').val();

        var dataObject = '{
                "command"     : "login",
                "username"    : "'+uname+'",
                "password"    : "'+pwd+'"
            }';

        $.ajax({
            type     : "POST",
            url      : "application/sys/sys_login.php",
            data     : dataObject,
            dataType : "json",
            success: function(response){
                if (response.status===true){
                    docCookies.setItem("sessionid", response.sessionid);
                    docCookies.setItem("username", response.username);
                    docCookies.setItem("roles", response.roles);
                    location.href = "application/index.php";

                }else{
                    console.log("false")
                    }           
                },
            beforeSend:function(){
                console.log("sending")
            }
        }).error(function() {
            console.log("error");
        });
        e.preventDefault();
    });
    });
于 2013-11-13T10:55:57.937 回答
0

你可以试试这个,确保你的 html 中有唯一的元素 ID,

<section >
    <form id="login">
        <input type="text" name="username" size="10" placeholder="username" />
        <input type="password" name="password" size="10" placeholder="password" />
        <input type="submit" name="login" value="Login" />
    </form>
</section>

 <script type="text/javascript">
 $(function(){
        $('#login').submit(function(e){

            var uname   =   $('input[name="username"]').val();
            var pwd     =   $('input[name="password"]').val();

            var dataObject = {
                    command     : "login",
                    username    : uname,
                    password    : pwd
                };

            $.ajax({
                type     : "POST",
                url      : "application/sys/sys_login.php",
                data     : dataObject,
                dataType : "json",
                success: function(response, status){
                    if (response.status===true){
                        docCookies.setItem("sessionid", response.sessionid);
                        docCookies.setItem("username", response.username);
                        docCookies.setItem("roles", response.roles);
                        location.href = "application/index.php";

                    }else{
                        console.log("false");
                        }           
                    },
                beforeSend:function(){
                    console.log("sending");
                }
            }).error(function() {
                console.log("error");
            });
            e.preventDefault();
        });
    });

参考:- http://api.jquery.com/submit/

于 2013-11-13T06:31:40.173 回答