javascript - XMLHTTP : Internal server error - issue with GET?

标签 javascript php mysql ajax internal-server-error

我正在尝试从存储在服务器上的 MySQL 数据库中检索一些数据。我使用 PHP、Javascript 和 AJAX 来获取数据。

当我在 Chrome 中运行 HTML 文件 (New.html) 并使用开发人员工具查看代码时,它显示:

GET http://example.net/Example/getuser.php?q=2 500 (Internal Server Error)

showUser @ New.html:31onchange @ New.html:52

我认为这是指 xmlhttp.onreadystatechange,并且 .send() 行旁边有一个红色的 X。

<html>
<head>
    <title>New</title>
    <meta charset="UTF-8">
                              //Javascript Code
<script> 
        function showUser(str) {
            if (str == " ") {
                document.getElementById("txtHINT").innerHTML = " ";
                return;
            } else {
                if (window.XMLHttpRequest) {
                    // code for IE7+, Firfox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();  
            } else {
                   // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("txtHint").innerHTML = this.reponseText;
                }
            };
            xmlhttp.open("GET","getuser.php?q="+str,true);
            xmlhttp.send(); //LINE 31
        }
    }
</script>
                      //CSS for HTML table
<style> 
    table         {
        width: 100%;
        border-collapse: collapse;
    }
    table, td, th {
        border: 1px solid black;
        padding: 5px;
    }   
    th            {
       text-align: left;
    }
</style>
</head>
                   <!-- Code for Form -->
<body>
    <form>
        <select name ="users" onchange="showUser(this.value)"> //LINE 51
            <option value=" ">Select a person:</option>
            <option value="1">Peter Griffin</option>
            <option value="2">Lois Griffin</option>
            <option value="3">Joseph Swanson</option>
            <option value="4">Glenn Quagmire</option>
        </select>
    </form>
    <br>
    <div id="txtHint"><b>MySQL Data should go here</b></div>
</body>
</html>

有谁知道如何解决这些问题吗?也许 .open() 需要放在代码的前面?或者也许需要某种处理程序?

PHP 文件:

<html>
<head>
    <title>Latest Attempt</title>
</head>
<?php
    $q = intval($_get['q']);
    // put your connection code here
    $servername = 'localhost';
    $username = 'user';
    $password = '12345678';
    $dbname = 'ajax_demo';
    
    // create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    
    mysqli_select($conn,"ajax_demo");
    $sql="SELECT * FROM my_DB WHERE id = '".$q."'";
    $result = mysqli_query($conn,$sql);
    
    echo "<table>
    <tr>
    <th>FirstName</th>
    <th>LastName</th>
    <th>Age</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>";
    while ($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['FirstName'] . "</td>";
        echo "<td>" . $row['LastName'] . "</td>";
        echo "<td>" . $row['Age'] . "</td>";
        echo "<td>" . $row['Hometown'] . "</td>";
        echo "<td>" . $row['Job'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    mysqli_close($conn);
?>
</html>

最佳答案

您的代码中有一些拼写错误,如果您修复它们,您的代码将完美运行:

在 JavaScript 中

在这一行

document.getElementById("txtHint").innerHTML = this.reponseText;

您拼写错误了 reponseText,它应该是 responseText,所以这一行将如下所示:

document.getElementById("txtHint").innerHTML = this.responseText;

在您的 PHP 中

您必须从页面顶部删除所有这些额外的 html 标签:

<html>
<head>
    <title>Latest Attempt</title>
</head>

以及页面底部的这个标签:

</html>

那么你在这一行:

$q = intval($_get['q']);

您必须以大写形式输入 $_GET,这样它就会像:

$q = intval($_GET['q']);

最后mysqli没有mysqli_select()函数,所以你必须完全删除这一行:

mysqli_select($conn,"ajax_demo");

现在你可以开始了:)

关于javascript - XMLHTTP : Internal server error - issue with GET?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40698763/

相关文章:

php - 通过 17gb xml 文件解析/扫描

php - mysql_query 在空表上返回 TRUE

mysql查询将行输出到列

javascript - FB.ui 回调未调用/FB.ui 流发布永久加载

javascript - 极小极大算法JS实现

javascript - react + Spring 启动: Can't get Authorization value from Header

php - 从同一个表中选择多行

javascript - 我想了解ReactJS中的SetState和Prevstate

php - set_include_path() 不适用于相对 require()

mysql - 数据库未正确存储重音字符