藍山帝景
2021-08-20 17:41:37
我正在嘗試在我的網站中構建一個新功能,這要求我從我在 Web 服務器上獲得的數據庫中檢索單個值。為此,我使用 php 腳本服務器端,它使用 msqli 從數據庫中檢索數據??蛻舳宋沂褂?Ajax 和 XMLhttpRequest 來調用 php 腳本。一切正常,我沒有任何錯誤,只是我的 xml 總是有一個空響應,這讓我認為我的 php 腳本有問題。你們能幫忙嗎?下面包含一些代碼。這是我的 php 腳本<?php$conn = new mysqli('localhost', 'nicolas', 'Password', 'RandomNumberHouse');if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); echo "error";} $HouseID = 6;$sql = "SELECT RandomVar FROM RandomVarHouse WHERE HouseID = $HouseID";$result = $conn->query($sql);$row = mysqli_fetch_array($result);$var = $row[2];echo $var;$conn-> close();?>這是我的 js 腳本function getRandomVar(){ xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","../inc/VarCommunication.php?"); xmlhttp.send(); xmlhttp.onreadystatechange = function() { if (this.readyState === 4 && this.status === 200) { var result = this.responseText; updateBackgroundColor(result); }; }} function updateBackgroundColor(number){ if( number < 100){ document.getElementById('mainBody').style.backgroundColor = 'red'; }}
1 回答
HUWWW
TA貢獻1874條經驗 獲得超12個贊
you miss place semicolon in xmlhttp request. please try below code and verify result
<script>
function getRandomVar()
{
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","demo1.php");
xmlhttp.send();
xmlhttp.onreadystatechange = function()
{
if (this.readyState === 4 && this.status === 200)
{
var result = this.responseText;
alert(result);
//updateBackgroundColor(result);
}
};
}
</script>
添加回答
舉報
0/150
提交
取消
