提交表单时没有发生ajax请求

时间:2016-03-22 02:41:47

标签: javascript php jquery ajax

我试图将Ajax用于表单,但似乎没有用。我不确定它是不是因为文件是php而是显示为javascript文件,或者其他类似的东西,但我想修复此问题。提前谢谢。

登录/ index.php的

<!doctype html>
<html>

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title><?php echo SERVER_NAME; ?> | <?php echo $this->title ?></title>

        <link rel="stylesheet" media="screen" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="<?php echo URL; ?>/public/css/bootstrap.min.css"></link>
        <link rel="stylesheet" href="<?php echo URL; ?>/public/css/css.login.php" />

        <script type="text/javascript" src="<?php echo URL; ?>/public/js/jquery.min.js"></script>
        <script type="text/javascript" src="<?php echo URL; ?>/public/js/bootstrap.min.js"></script>
    </head>

    <body>

        <div id="login" class="container">
            <div class="module">
                <div class="module-header">
                    <h4>Login</h4>
                </div>
                <div class="module-body module-padding">
                    <form class="form" method="post" action="<?php echo URL;?>/actions/login.php">
                        <input type="text" name="login-username" placeholder="Username">
                        <br />
                        <input type="password" name="login-password" placeholder="Password">
                        <br />
                        <input type="submit" name="submit" placeholder="Login" class="pull-right">
                    </form>
                </div>
            </div>
            <div class="msg">

            </div>
        </div>

        <script type="text/javascript" src="<?php echo URL; ?>/public/js/user-login.php"></script>

    </body>
</html>

user-login.php(充当js)

<?php

    header('Content-Type: application/javascript');

    require "../../Application/Configuration/config.php";

?>

$(window).load(function() {
    $('.form').submit(function(event) {
        var formData = {
            'login-username': $('input[name=login-username]').val(),
            'login-password': $('input[name=login-password]').val()
        };

        $.ajax({
            type: "POST",
            url: "<?php echo URL; ?>/actions/login.php",
            data: formData,
            success: function (json_data) {
                var data_array = $.parseJSON(json_data);
                if (data_array['error'] == false) {
                    $('.msg').css('display', 'block');
                    $('.msg').addClass('msg-success');
                    $('.msg p').html(data_array['text']);
                    $('.msg').fadeOut(4000);
                    var delay = 4000;
                    setTimeout(function() {
                        window.location.replace("../index.php");
                    }, delay);
                } else {
                    $('.msg').css('display', 'block');
                    $('.msg').addClass('msg-failure');
                    $('.msg p').html(data_array['text']);
                    $('.msg').fadeOut(4000);
                }
            },
            fail: function () {
                alert("failed to send");
            }
        });
    });
});

0 个答案:

没有答案