dthp96899 2014-12-01 15:35
浏览 29
已采纳

无法向PHP文件发出Ajax请求

I'm using the following code to send Ajax request to a PHP file:

var d = "email=" + email + "&password=" + password;

        $.ajax({
            url: "login/c_login_form.php",
            data: d,
            type: "POST",
            success: function(str) {

                        if ( str === "#" ) { //# means success
                            alert("Login successful!");
                            return;
                        }
                        else {
                            //login failed
                            alert("Error logging in: " + str);
                            return;
                        }
                     },
            error: function(obj) {
                        alert("Failed to send Ajax request.");
                        return;
                     }
        });

The contents of the PHP file are:

<?php
/*
*
* The controller file for login form
*
*/

if( isset( $_POST["email"] ) && isset( $_POST["password"] )) {

    $email = $_POST["email"];
    $password = $_POST["password"];

    //load the config file to read settings
    require_once($_SERVER['DOCUMENT_ROOT'] . '/hrms/lib/config.php');

    //connect to database
    $conn = mysqli_connect($db_host, $db_username, $db_password, $db_name);

    if(!$conn) {
        echo "Can't connect to database";
        return;
    }

    //check if employee is active
    $query = "SELECT employee.emp_id, role, is_active FROM employee INNER JOIN user ON employee.emp_id = user.emp_id WHERE email_bb = '{$email}' AND password = '{$password}'";

    if( $query === FALSE ) {
        echo "Failed to query database.";
        return;
    }

    $result = mysqli_query($conn, $query);

    if( $result === false ) {
        echo "No such user exists. Please re-check login details.";
        return;
    }

    $row = mysqli_fetch_assoc($result);

    if( (count($row) > 0 ) and ($row['is_active'] == "0") ) {
        echo "Employee not active anymore. Please contact HR.";
        return;
    }

    if( count($row) === 3 ) {
        //Everything looks okay. Process login now.
        $emp_id = $row['emp_id'];
        $role = $row['role'];

        //close connection
        mysqli_close($conn);

        session_start();
        $_SESSION['emp_id'] = $emp_id;
        echo "#";

        $path = $_SERVER['DOCUMENT_ROOT'] . '/hrms/dashboard.php?role={$role}';
        header("Location://{path}");
        die();      
    }
}
else {
    echo "Error. POST is not set.";
    return;
}

Strangely enough, if I make the first two statements in the PHP file to be echo "#"; return; then I'm able to see the "Login successful" message. Otherwise, even when I send the correct query (verified in phpMyAdmin), I keep getting the error saying "Failed to send Ajax request".

Any idea what might be causing it?

  • 写回答

1条回答 默认 最新

  • dpw5865 2014-12-06 17:01
    关注

    Posting an answer so as to close this question. The problem was indeed related to headers already being sent, as I was using the echo and header functionalities in the same place. When I removed the header part and performed the redirection from JavaScript, it worked as expected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)