yujx1217 2015-02-16 08:11 采纳率: 100%
浏览 1884
已采纳

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对象时发现不可以用,请问有什么解决方法呢?

  • 写回答

1条回答 默认 最新

  • danielinbiti 2015-02-16 10:10
    关注

    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){
    //...
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置