javascript - 从 Ajax 调用返回 php 错误

标签 javascript php jquery ajax

我有一个 php 脚本,我希望用 ajax 调用它,我遇到了来自 ajax 的错误,显示“Error:email=my_email&password=myPassword”。

这是 PHP 脚本

<?php

session_start(); //starts a session in order to be-able to save session variables and to read them

require "db_config.php"; //Allows us to use the database connection from db_config.php in this file

if ($_SERVER["request_method"] == "post"){ //checks if the form was submitted

    $email = $_POST["email"]; //fetching the email address which was inserted in the login.html form
    $password = $_POST["password"]; //fetching the password which was inserted in the login.html form

    /*

    querying the database, to check whether there is a result with the email and password entered by the user

    */

    $checkForUser = mysqli_query($db_connection, "SELECT * FROM `tbl_users` WHERE email = '$email' and Password = '$password' LIMIT 1");

    /*

    checking if the query resulted in one row, if there is a row
    it means there is a user with this email and password, which means these are the correct creadentials

    */

    $rows = mysqli_num_rows($db_connection, $checkForUser);

    if ($rows == 1){ 

        //this means: correct credentials

        //the next few lines fetch the information from the result
        while($row = mysqli_fetch_assoc($checkForUser)){
            $_SESSION["user_id"] = $row["userId"]; //creates a session variable containing the users id
            $_SESSION["users_name"] = $row["firstName"]. " ".$row["lastName"]; //creates a session variable containing the users name
        }

        echo "You are now logged in: ". $_SESSION["users_name"];

    }else{

        //this means: incorrect credentials
        echo "Incorrect Username or password"; //prints out error message

    }

}

?>

这是main.js

$(document).ready(function() {

    $('#loginForm').submit(function() {
        var data = $(this).serialize();

        $.ajax({
        url: "../php/login.php",
        type: "POST",
        data: data,
            success: function(data) {
                $('*').html(data);
            },
            error: function() {
                alert('ERROR: ' + data);
            }
        });

        return false;

    });

});

这是login.html页面,可能会有帮助

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <link rel="stylesheet" type="text/css" href="css/responsive.css">
    <script src="js/jquery.js"></script> 
    <script src="js/main.js"></script>
    <meta name="copyright" content="Yudi Moszkowski">
    <title>Login | Your Site Name</title>
</head>
<body>
    <div id="loginContainer">
        <div class="logo"><img src="img/yourLogo.png"></div>
        <form action="php/login.php" method="post" id="loginForm">
            <input type="text" name="email" placeholder="Email" id="loginEmail" class="loginInput" required="true">
            <input type="password" name="password" placeholder="Password" id="loginPassword" class="loginInput" required="true"/>
            <input type="submit" value="Login" name="loginSubmit" id="loginSubmit">
        </form>
        <div id="loginOptions"><p id="noAccount">Not signed up? <a href="signup.html">Signup</a></p><p id="forgotPass"><a href="forgot_pass.html">Forgot password?</a></p></div>
    </div>
</body>
</html>

感谢您的宝贵时间:)

最佳答案

您似乎输入了错误的网址: 在 html 操作中,您使用“php/login.php”,在 ajax 调用中,您使用相同的 url,前面带有“../”。如果您解释一下login.php和这个html文件的位置,这将有助于解决您的问题。

关于javascript - 从 Ajax 调用返回 php 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37260761/

相关文章:

php - 我在设置 laravel 时发生了什么错误

php - 如何在 PHP 中删除空格后的所有内容?

javascript - 你如何在jquery中移动一个div元素

javascript - jquery ui 可在旋转的 div 中拖动

javascript - PHP PDF密码保护(无密码不可打开)

javascript - 合并 JSON 对象数组并使用 Javascript 排序

javascript - jQuery 1.6 浏览器检测

php - Paypal Sandbox 如何检查计费计划和计费协议(protocol)

javascript - Jquery Multiselect - 手动触发事件?

jquery - 如何将部分内容变灰或将图像 "on top"隐藏