【问题标题】:PHP not returning anything to ajax if class is instantiated如果类被实例化,PHP不会向ajax返回任何内容
【发布时间】:2018-05-01 06:18:08
【问题描述】:

首先,对不起标题,我不知道如何命名我面临的问题。我会尽力解释。我有一个 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) { 没有被调用。

【问题讨论】:

  • 您在哪里以及如何“实例化像 $MyClass = new MyClass(); 之类的类”?
  • 在顶部。我没有将它添加到代码中
  • 在什么的顶部?为什么?你想在课堂上做什么?
  • 在我的登录文件的顶部。我正在尝试从类中调用方法
  • 应该不重要,我只是很困惑为什么 ajax 中的成功功能会停止工作,所有其他的东西都运行良好,php 正在完全执行 -> 用户已登录,但是没有调用ajax成功函数中的代码

标签: javascript php ajax


【解决方案1】:

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

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

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

【讨论】:

  • 会不会是因为我的类的构造函数是空的?
猜你喜欢
  • 1970-01-01
  • 2011-05-12
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
  • 2017-04-30
  • 2016-10-26
  • 2020-02-22
相关资源
最近更新 更多