JQuery / Ajax Post返回了数据和IF函数

时间:2013-08-09 13:17:07

标签: jquery ajax if-statement

这是我的main.js文件中的内容:

$("#login-form").submit(
        function() {

            $("#message").removeClass().addClass('messagebox').html(
                    '<img src="public/images/loading.gif" />Validating....').fadeIn(1000);

            $.post("login/run", {
                username : $('#username').val(),
                password : $('#password').val()
            }, function(data) {
                if (data == 'yes') {
                    // add message and change the class of the box and start
                    // fading
                    $("#message").fadeTo(
                            200,
                            0.1,
                            function() // start fading the messagebox
                            {

                                $(this).html('Logging In...').addClass(
                                        'messageboxok').fadeTo(900, 1,
                                        function() {
                                            // redirect to secure page
                                            document.location = 'secure.php';
                                        });
                            });

                } else {
                    alert(data);
                    $("#message").fadeTo(
                            200,
                            0.1,
                            function() // start fading the messagebox
                            {


                                    // add message and change the class of the
                                    // box and start fading
                                    $(this).html('Your username or password is incorrect')
                                            .addClass('messageboxerror')
                                            .fadeTo(900, 1);


                            });
                }
            });
            return false;
        }
);

这是它引用的函数(当前设置为始终回显yes以强制获得肯定结果)

public function run() {
    echo 'yes';
}

我遇到的问题是当我进行警报(数据)时,警报显示我得到的输出是正确的,但IF函数只是在执行ELSE部分而不管数据是否正确。< / p>

另一个令人困惑的部分是,这个代码在我昨晚睡觉前工作正常 - 就在现在它没有。

我的语法在任何地方都有问题吗?


编辑1

修改它: 将,'json'添加到$ .post区域的末尾,并将 json_encode 添加到echo

main.js:

//login form ajax
$("#login-form").submit(
        function() {

            $("#msgbox").removeClass().addClass('messagebox').html(
                    '<img src="public/images/loading.gif" />Validating....').fadeIn(1000);

            $.post("login/run", {
                username : $('#username').val(),
                password : $('#password').val()
            }, function(data) {
                if (data == 'yes') {
                    // add message and change the class of the box and start
                    // fading
                    $("#msgbox").fadeTo(
                            200,
                            0.1,
                            function() // start fading the messagebox
                            {

                                $(this).html('Logging In...').addClass(
                                        'messageboxok').fadeTo(900, 1,
                                        function() {
                                            // redirect to secure page
                                            document.location = 'secure.php';
                                        });
                            });

                } else {
                    alert(data);
                    $("#msgbox").fadeTo(
                            200,
                            0.1,
                            function() // start fading the messagebox
                            {


                                    // add message and change the class of the
                                    // box and start fading
                                    $(this).html('Your username or password is incorrect')
                                            .addClass('messageboxerror')
                                            .fadeTo(900, 1);


                            });
                }
            }, 'json');
            return false;
        });
//end login form ajax

和功能:

public function run() {
        echo json_encode('yes');

}

编辑2

更改为$ .ajax,使该函数返回JSON编码数组

main.js:

$("#login-form").submit(
        function() {

            $("#msgbox").removeClass().addClass('messagebox').html(
                    '<img src="public/images/loading.gif" />Validating....').fadeIn(1000);


            $.ajax({
                url: 'login/run',
                dataType: 'json',
                type: 'POST',
                data: {
                    username : $('#username').val(),
                    password : $('#password').val()
                },
                success: function(data){
                    alert(data.result);


                    if (data.result == 'yes') {
                        // add message and change the class of the box and start
                        // fading
                        $("#msgbox").fadeTo(
                                200,
                                0.1,
                                function() // start fading the messagebox
                                {

                                    $(this).html('Logging In...').addClass(
                                            'messageboxok').fadeTo(900, 1,
                                            function() {
                                                // redirect to secure page
                                                document.location = 'secure.php';
                                            });
                                });

                    } else {
                        alert(data);
                        $("#msgbox").fadeTo(
                                200,
                                0.1,
                                function() // start fading the messagebox
                                {


                                        // add message and change the class of the
                                        // box and start fading
                                        $(this).html('Your username or password is incorrect')
                                                .addClass('messageboxerror')
                                                .fadeTo(900, 1);


                                });
                    }



                    //return false; 
                }
            });


            return false;
        });

功能:

public function run() {
    $result = array('result' => 'yes');
    echo json_encode($result);  
}

0 个答案:

没有答案