未捕获的ReferenceError:xmlhttp未定义?

时间:2022-11-07 07:41:46
<html>
<head>
    <title>List</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Javascript code -->
<script>
         function showUser(str) {
         if (str == " ") {
         document.getElementById("txtHint").innerHTML = " ";
         return;
         } else {
         if (window.XMLHttpRequest) {
         // code for IE7+, Firefox, Chrome, Opera, Safari
         } 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.responseText;
         }
         };
         xmlhttp.open("GET","getuser.php?q="+str,true);
         xmlhttp.send();
         }
        }
</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>
<body>

    <form>
        <select name="users" onchange="showUser(this.value)">
            <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>
    <div id="txtHint">Result from PHP script should appear here</div>
 </body>
 </html>

When I run the following HTML page in the Google Chrome Browser via Netbeans, I am met with this error (see Title) when I try to select a person from the list.

当我通过Netbeans在Google Chrome浏览器中运行以下HTML页面时,当我尝试从列表中选择一个人时,我遇到了此错误(请参阅标题)。

xmlhttp.onreadystatechange = function()

xmlhttp.onreadystatechange = function()

This line of code and the one below seems to be the areas of concern based on Chrome's Developer tools.

这行代码和下面的代码似乎是基于Chrome开发人员工具的关注领域。

select name="users" onchange="showUser(this.value)

select name =“users”onchange =“showUser(this.value)

Can anyone pinpoint what needs to be changed?

任何人都可以找出需要改变的内容吗?

1 个解决方案

#1


1  

Right underneath:

正下方:

// code for IE7+, Firefox, Chrome, Opera, Safari

Add:

加:

xmlhttp = new XmlHttpRequest();

xmlhttp = new XmlHttpRequest();

That way, you'll satisfy web browsers with javascript engines that have XMLHttpRequest defined.

这样,您就可以使用定义了XMLHttpRequest的javascript引擎来满足Web浏览器的需求。

Also, xmlhttp needs to have a valid value (handle) before "xmlhttp.onreadystatechange = function()" can be properly executed.

此外,在“xmlhttp.onreadystatechange = function()”可以正确执行之前,xmlhttp需要有一个有效值(句柄)。

If your browser (especially very old IE browsers) is still picky then change "xmlhttp" to "var xmlhttp" since var before a variable name means define a new variable.

如果您的浏览器(特别是非常旧的IE浏览器)仍然挑剔,那么将“xmlhttp”更改为“var xmlhttp”,因为变量名之前的var意味着定义一个新变量。

#1


1  

Right underneath:

正下方:

// code for IE7+, Firefox, Chrome, Opera, Safari

Add:

加:

xmlhttp = new XmlHttpRequest();

xmlhttp = new XmlHttpRequest();

That way, you'll satisfy web browsers with javascript engines that have XMLHttpRequest defined.

这样,您就可以使用定义了XMLHttpRequest的javascript引擎来满足Web浏览器的需求。

Also, xmlhttp needs to have a valid value (handle) before "xmlhttp.onreadystatechange = function()" can be properly executed.

此外,在“xmlhttp.onreadystatechange = function()”可以正确执行之前,xmlhttp需要有一个有效值(句柄)。

If your browser (especially very old IE browsers) is still picky then change "xmlhttp" to "var xmlhttp" since var before a variable name means define a new variable.

如果您的浏览器(特别是非常旧的IE浏览器)仍然挑剔,那么将“xmlhttp”更改为“var xmlhttp”,因为变量名之前的var意味着定义一个新变量。