如果类被实例化,PHP不会向ajax返回任何内容

时间:2018-04-30 19:26:15

标签: javascript php ajax

首先,抱歉标题,我不知道如何命名我面临的问题。我会尽力解释。我有一个php文件,它可以像这样处理用户登录:

session_start();
if(isset($_POST['do_login'])) {
    require_once '../inc/mysql_connect.php';
    $uname = $_POST['username'];
    $pass = $_POST['password'];
    $uname = strip_tags(mysqli_real_escape_string($con,trim($uname)));
    $pass = strip_tags(mysqli_real_escape_string($con, trim($pass)));
    $sql = "SELECT * from users where username='".$uname."'";
    $select_data = mysqli_query($con,$sql)or die(mysqli_error());

    if (mysqli_num_rows($select_data)>0) {
        $row = mysqli_fetch_array($select_data);
        $password_hash = $row['password'];
        $token = $row['token'];
        $email = $row['email'];
        $email_verified = $row['email_verified'];

        if (password_verify($pass,$password_hash)) {
            if ($email_verified > 0) {
                setcookie('token', $token, time()+31556926 , "/", "example.com", true, true);
                echo "success";
            } else {
                echo "email_not_verified";
                $_SESSION['email_not_verified'] = "true";
                $_SESSION['username'] = $uname;
                $_SESSION['email'] = $email;
                $_SESSION['token'] = $token;
            }
        } else {
            echo "fail";
        }

        exit();
    }
}

我有一个js文件向文件发送请求:

function do_login()
{
    $("#btn-login").addClass('disabled');
    var username=$("#emailid").val();
    var pass=$("#password").val();

    if(username!="" && pass!="") {
        $.ajax({
            type:'post',
            url:'https://example.com/core/auth/do_login.php',
            data:{
                do_login:"do_login",
                username:username,
                password:pass
            },
            success: function (response) {
                if (response =="success") {
                    $('#login').modal('hide');
                    window.location.href="https://example.com/account/";
                } else if (response=="email_not_verified") {
                    window.location.href="https://example.com/login"
                } else {
                    shakeModal();
                    $("#btn-login").removeClass('disabled');
                }
            }
        });
    }

    return false;
}

但是如果我实例化像$MyClass = new MyClass();这样的类,PHP代码可以工作,但Ajax成功:function (response) {没有被调用。

1 个答案:

答案 0 :(得分:2)

您的代码可能会失败,因此永远不会调用您的success:块。 尝试在您的成功之下添加以下内容:阻止:

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

我使用this作为错误块的基础。