Php 为什么我的AJAX请求会打印出整个HTML代码?

Php 为什么我的AJAX请求会打印出整个HTML代码?,php,html,ajax,forms,Php,Html,Ajax,Forms,我试图发出一个AJAX请求,将数据发送到一个PHP文件,检查用户名和密码是否正确,返回一个答案,然后运行login.PHP文件,该文件决定会话的运行 现在,我的AJAX请求除了打印登录页面的整个HTML代码外,什么都不做 如果有人知道原因,请告诉我 (我为发布我所有的login.js和checkLogin.php提前道歉,我担心如果没有两者的上下文,这个问题可能无法回答) 这是login.js $(function() { //Once the document has fully loaded

我试图发出一个AJAX请求,将数据发送到一个PHP文件,检查用户名和密码是否正确,返回一个答案,然后运行login.PHP文件,该文件决定会话的运行

现在,我的AJAX请求除了打印登录页面的整个HTML代码外,什么都不做

如果有人知道原因,请告诉我
(我为发布我所有的login.js和checkLogin.php提前道歉,我担心如果没有两者的上下文,这个问题可能无法回答)

这是login.js

$(function() { //Once the document has fully loaded 

    $("#login_form").submit(function(event) {

        //INITIALIZING VARIABLES
        var userName = $("#userName").val();
        var passWord = $("#passWord").val();
        var error = 0;

        event.preventDefault(); //Prevent normal submit action
        $("#userError, #passError").text(""); //Clear the text each time the user clicks submit

        if (userName == "") { //Checking if the username is blank
            $("#userError").text("Please enter your username");
            error = 1;
        }

        if (passWord == "") { //Checking if the password is blank
            $("#passError").text("Please enter your password");
            error = 1;
        }

        //BEGIN Ajax Request
        var $form = $(this),
            term = $form.find("userName, passWord"),
            url = 'checkLogin.php';

        var posting = $.post(url, {username: userName, password: passWord});

        posting.done(function(data) {
            $("#userError").text(posting.responseText);
        });
        //END Ajax Request

        if (error == 0) {
            $("passError").text("Success");
        }
    }); //END submit button click   

    $("#login_form").submit();

});
这是checkLogin.php

<?php
    $link = mysqli_connect(DB info private); //Database Connection

    if (!$link) { 
        die('Could not connect: ' . mysqli_connect_error()); 
    }

    if ($_SERVER['REQUEST_METHOD'] === 'POST') { //Keeps the text inside the this.responseText
        //Preparing and Binding SQL statement
        $stmt = mysqli_prepare($link, "SELECT username, password FROM login WHERE username =? and password =?");
        mysqli_stmt_bind_param($stmt, "ss", $username, $password);

        //PRINTS UNIDENTIFIED INDEX
        $username = $_POST['username']; //Retrieving the username 
        $password = $_POST['password']; //Retrieving the password
        mysqli_stmt_execute($stmt); //Execute the parameterized prepared statement

        $result = mysqli_stmt_get_result($stmt);
        $row = mysqli_fetch_assoc($result);
    }
?>

来自client_login.php的表单标记

<form action="login.php" method="post" name="login_form" id="login_form">
</form>

您没有在checkLogin.php上返回任何内容。你应该试试:

$row = mysqli_fetch_assoc($result);
if($row) {
    echo "User found!";
    exit();
}
我建议您以json格式返回文本。通过这种方式,您可以返回更复杂的响应以在js脚本中使用。例如,在checkLogin.php中:

$row = mysqli_fetch_assoc($result);
if($row) {
    echo json_encode([
        'code' => '1', // code that represent successful
        'message' => 'User found!'
    ]);
    exit();
}
在您的login.js中:

posting.done(function(data) {
    var response = JSON.parse(data);
    if(response.code == '1') {
        $("#userError").text(response.message);
    }
});

这里没有什么问题

  • url=checkLogin.php
    应该是
    url='checkLogin.php'

    现在url将是
    未定义的
    ,ajax url将把所有内容发布到
    yoursite.com/undefined
    ,服务器将返回404,默认404页面。这就是为什么您在响应中看到html

  • 您的
    checkLogin.php
    应该向客户端返回一些数据。为此使用
    echo()


  • 你好。首先,不必为发布你的所有代码而道歉,你发布的越多,就能有越多的人来帮助你!同样,在代码的第一眼,您并没有从“checkLogin.php”返回任何内容。您应该在“if”语句中添加一个return,检查它是否是POST请求,它返回您在“login.js”中的“posting.done(…)”函数中查找的数据。谢谢,这是一个疏忽,我已经修复了它