AJAX状态200在json_encode的PHP echo上触发错误

时间:2016-12-05 01:47:02

标签: javascript php json ajax

以下程序一切正常,但AJAX错误引发:

的javascript:

var data = {
    email: 'me@gmail.com',
    password: 'secretword'
};

$.ajax({
    type: "POST",
    dataType: "application/json",
    url: "http://localhost/CFBserver/validateUser.php",
    data: data,
    success: function (response) {
        console.log(response.responseText);
    },
    error: function (response) {
        console.log(response.responseText);
    }
});

}

PHP:

    <?php

    $conn = mysqli_connect('localhost', 'root', '', 'cfbdata');
    if (mysqli_connect_errno($conn)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
    $password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);

    $sql = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";

    if (!mysqli_query($conn, $sql)) {
        die('Error: ' . mysqli_error($conn));
    }

    $result = mysqli_query($conn, $sql);
    $numrows = mysqli_num_rows($result);
    if ($numrows > 0) {
        $message = array('result' => 'found',
            'email' => $email,
            'password' => $password,
        );
    } else {
        $message = array('result' => 'Not found',
            'email' => $email,
            'password' => $password,
        );
    }
    header('Content-type: application/json');
    echo json_encode($message);

    mysqli_close($conn);
    ?>

这是控制台显示的内容:

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        {"result":"found","email":"me@gmail.com","password":"secretword"}
    </body>
</html>

因此php在mysql数据库中找到记录,但在返回AJAX时,会触发错误。这是为什么?

0 个答案:

没有答案