php - 如何在AJAX中显示MYSQL错误?

标签 php mysql ajax

我正在使用 PHP-MYSQL 学习 AJAX。
我有三个文件index.php、process.php 和database.php...
JavaScript 包含在index.php 中。

index.php

<main>
    <header>
        Displaying MYSQL errors
    </header>
    <!-- Display error -->
    <section id="error"></section>
    <div id="submit_div">
        <button id="submit" name="submit_btn">Submit</button>
    </div>
</main>

Javascript

<script>
    let submit_btn = document.getElementById('submit');
    let error = document.getElementById('error');

    function showHint() {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "process.php", true);
        xhr.setRequestHeader('X-Requested-with', 'XMLHttpRequest');

        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var simple_text = xhr.responseText;
                error.innerText = simple_text; //print the error.
            } // if
        } // onreadystatechange()

        // Sending the request
        xhr.send();
    } //showHint()

    submit_btn.addEventListener('click', function() {
        showHint();
    }, false);

</script>

process.php

<?php

    require_once('database.php');

    $db = db_connect();

?>

数据库.php

<?php
define("DB_SERVER", "localhost");
define("DB_USER", "abhishek");
define("DB_PASS", "wrongpassword"); // I set the wrong password to print the error.
define("DB_NAME", "ajax");

function db_connect(){
    $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
    confrim_db_connect();
    return $connection;
}

function confrim_db_connect(){
    if(mysqli_connect_errno()){
        $msg = "Database connection failed: ";
        $msg .= mysqli_connect_error();
        $msg .= " (" . mysqli_connect_errno() . ")";
        exit($msg);
    }
}
?>

现在单击按钮,ajax 请求启动并将请求发送到 process.php 页面,该页面在 xhr.responseText 中返回响应。
请注意,在database.php中,我为数据库提供了错误的密码以获取错误作为响应。
所以当我按下按钮来获取 xhr.responseText 的内容时。
它给出了 xhr.responseText 的内容(这就是我想要的)-

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'abhishek'@'localhost' (using password: YES) in /var/www/html/AJAX/sql_ajax/database.php on line 5
Database connection failed: Access denied for user 'abhishek'@'localhost' (using password: YES) (1045)

我还没有问题。
但是如果我解析 JSON 中的结果使用 var in_json = JSON.parse(xhr.responseText);
然后它给了我错误 -

VM949:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at show_json ((index):79) at XMLHttpRequest.xhr.onreadystatechange ((index):58)

所以问题是json无法解析结果。
所以现在我的问题是 -
如何解析上面的json结果?
我已经知道如何打印 jquery 中的错误.

这里有关于 javascript 的帮助...

请再次阅读我的问题,我添加了更多信息...

最佳答案

根据评论部分与@AlbertoSinigaglia的讨论,我发布了解决方案。
所以 xhr.onreadystatechange() 函数看起来像 -

xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
        try { let result = JSON.parse(xhr.responseText); }
        catch(e) { alert(e); // error in the above string (in this case, yes)! 
        }
    } // if
} // onreadystatechange()

关于php - 如何在AJAX中显示MYSQL错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59516036/

相关文章:

javascript - 将表单的复选框值作为 JSON 传递?

javascript - 当 Mysql 数据库更改状态时使用 Push.js 推送通知

php - PECL 和 PEAR 有什么区别?

php - php和mysql的年度警报机制

php - laravel Eloquent 模型更新事件未被触发

MySQL存储过程导致错误

php - MYSQL 是这个 JOIN 太冗长还是这是唯一的方法?排序不起作用

php - mysql 中的 NULL 或 'string' 在 php 中插入查询字符串

java - 如何更新JTable中的数据? (有代码)

JavaScript、AngularJS - 发送多个同时的 ajax 调用