亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

XMLhttpRequest 返回空響應的問題

XMLhttpRequest 返回空響應的問題

藍山帝景 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> 


查看完整回答
反對 回復 2021-08-20
  • 1 回答
  • 0 關注
  • 519 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號