php ajax请求未给出任何响应或错误

时间:2019-09-18 13:32:53

标签: php ajax

当用户输入电子邮件并单击按钮时,我一直在尝试根据注册的电子邮件获取用户名。我向ajax请求提供了两个数据,第一个是[cf],它标识应处理代码的哪一部分其次是电子邮件,但是当单击按钮时,什么也没有发生,并且只有当我打开具有ajax的php代码的页面时,控制台中才会出现错误,它会显示“注意:未定义的变量:funcCall in D:\ XAMPP \ htdocs \第19行的lahj \ framework \ signin.php“
在这里,我将提供有关ajax的代码:

$("#login-next-btn").click(function(){
        if(!$("#signin-email").val().trim()){
            var email = $("#signin-email").val().trim();
            $.ajax({
                url: "framework/signin.php",
                type: "POST",
                data: {cf:'cf1',email:email},
                dataType:"json",
                success: function(data){
                    if(data.result != "0")
                    {
                        alert("helo");
                        $("#signin-box-header p:first-of-type").text("مرحبًا");
                        $("#signin-box-header p:last-of-type").remove();
                        $("#signin-box-header").append("<p id='sigin-email-display'>"+data.result+"</p>");
                        $("#first-loader").fadeOut(600,function(){
                            $("#second-loader").css({"display":"flex"}).fadeIn(400);
                        });
                    }
                    else
                    {
                        alert("fail");
                    }
                }
            });
        }
    });

这是我的php代码,位于一个名为framework的文件夹中的一个名为signin.php的文件中:

<?php
ini_set( 'error_reporting', E_ALL );
ini_set( 'display_errors', true );
include_once 'db.php';

$email = null;
$funcCall = null;

if(isset($_POST['email']))
{
    $email = $_POST['email'];
}

if(isset($_POST['cf']))
{
    $funcCall = $_POST['cf'];
}

if($funcCall == 'cf1' && !empty($email))
{
    try
    {
        $database = new db();
        $db = $database->openConnection();
        $stmt = $db->prepare("select userName from khUsers where email = ?");
        $stmt->execute(array($email));
        $usersName = $stmt->fetchColumn();
        $data = array('result' => $usersName);
        echo json_encode($data);
    }
    catch (PDOException $e)
    {
        echo "There is some problem in connection: " . $e->getMessage();
        $data = array('result' => "0");
        echo json_encode($data);
    }
}
?>

1 个答案:

答案 0 :(得分:-1)

在json_encode()函数的末尾添加exit()函数。它将在控制台中向您显示请求的结果/错误。

if($funcCall == 'cf1' && !empty($email))
{
    try
    {
        $database = new db();
        $db = $database->openConnection();
        $stmt = $db->prepare("select userName from khUsers where email = ?");
        $stmt->execute(array($email));
        $usersName = $stmt->fetchColumn();
        $data = array('result' => $usersName);
        echo json_encode($data); exit();
    }
    catch (PDOException $e)
    {
        echo "There is some problem in connection: " . $e->getMessage();
        $data = array('result' => "0");
        echo json_encode($data); exit();
    }
}