Javascript如何传递XMLHttpRequest对象。

function createXMLHttpRequest() {
try {
return new XMLHttpRequest();
} catch(e) {
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
throw e;
}
}
}
}
window.onload=function (){
//创建,连接,发送请求,接收数据
var xmlHttp=createXMLHttpRequest();
xmlHttp.open("GET","/ajax/PServlet",true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=load(xmlHttp);
};

function load(xmlHttp){

if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
//...
}
}

我在load函数里面使用xmlHttp对象时发现不可以用,请问有什么解决方法呢?

onreadystatechange应该赋值的是函数名也就是xmlHttp.onreadystatechange=load
把xmlHttp定义成全局变量

var xmlHttp;
 window.onload=function (){
//创建,连接,发送请求,接收数据
 xmlHttp=createXMLHttpRequest();
xmlHttp.open("GET","/ajax/PServlet",true);
xmlHttp.send(null);
xmlHttp.onreadystatechange=load;
};
function load(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
//...
}
}