[英]XMLHTTP: Internal server error - issue with GET?
我正在嘗試從存儲在服務器上的MySQL數據庫檢索一些數據。 我正在使用PHP,Javascript和AJAX來獲取數據。
當我在Chrome中運行HTML文件(New.html)並使用開發人員工具查看代碼時,它說:
GET http://example.net/Example/getuser.php?q=2500 (內部服務器錯誤)
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");
現在你可以走了:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.