doumu1873 2016-11-15 23:26
浏览 62
已采纳

未捕获的ReferenceError:xmlhttp未定义?

<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.

xmlhttp.onreadystatechange = function()

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

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

Can anyone pinpoint what needs to be changed?

  • 写回答

1条回答 默认 最新

  • douxian1895 2016-11-15 23:35
    关注

    Right underneath:

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

    Add:

    xmlhttp = new XmlHttpRequest();

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

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?