我是javascript的新手,特别是对ajax ...而且只是想弄清楚..
我从一个教程中编写了这段代码,无法找到我做错了什么。 Here you can see it live我从Firebug得到的错误:“TypeError:xmlhttp未定义 [打破此错误]
if(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
答案 0 :(得分:3)
你这里有一个错字:
xmmlhttp= new XMLHttpRequest();
^
更改为
xmlhttp= new XMLHttpRequest();
正如迈克尔指出的那样,在分配你的onreadystatechange函数时你有括号:
xmlhttp.onreadystateshange=stateChanged();
^ remove the ()
如果不删除括号,将调用stateChange()
函数,返回值将提供给您不想要的xmlhttp.onreadystateshange
。
答案 1 :(得分:0)
属性名称中存在错误类型:
xmlhttp.onreadystateshange=stateChanged;
必须是'onreadystatechange'
。