繁体   English   中英

xmlhttp是未定义的。 Javascript Ajax

[英]xmlhttp is undefined. Javascript Ajax

我是javascript的新手,尤其是ajax。只是想弄清楚。

我从一个教程中编写了这段代码,找不到我做错了什么。 在这里你可以看到它的现场

我从Firebug中收到的错误:“ TypeError:xmlhttp是未定义的[此错误中断]

如果(xmlhttp.readyState == 4){“

我的代码是

// JavaScript Document

var xmlhttp;
var url;

function ajaxFunction(){

if  (window.ActiveXObject){//if the window is InternetExplorer

    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    }else if(window.XMLHttpRequest){// if Window is Firefox etc..

        xmmlhttp= new XMLHttpRequest();

        }else{

            alert ("Get a New Browser")

            }

}//end of ajaxFunction()


function getInfo(){

    ajaxFunction();

    var entryInfo= document.getElementById("entry").value;

            function stateChanged(){
                if (xmlhttp.readyState == 4){

                    document.getElementById("results").innerHTML = xmlhttp.responseText;

                    }//if (xmlhttp.readyState == 4)

                }//end of function stateChanged()


url = "info.php?user="+entryInfo;
xmlhttp.onreadystateshange=stateChanged();
xmlhttp.open("GET",url,true);
xmlhttp.send(null);


    }// end of function getInfo

您在这里有错字:

   xmmlhttp= new XMLHttpRequest();
     ^

改成

 xmlhttp= new XMLHttpRequest();

同样正如Michael指出的那样,在分配onreadystatechange函数时,您需要加上括号:

xmlhttp.onreadystateshange=stateChanged();
                                       ^ remove the ()

如果不删除括号,则将调用stateChange()函数,并将返回值提供给不需要的xmlhttp.onreadystateshange

属性名称中存在错误的类型: xmlhttp.onreadystateshange=stateChanged;

它必须是'onreadystatechange'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM