ajax异步调用,回调函数的问题,this未定义

来源:百度知道 编辑:UC知道 时间:2024/06/11 01:08:00
代码如下:
function getHttpRequest()
{
var request=false;
if(window.XMLHttpRequest)
request=new XMLHttpRequest();
else if(window.ActiveXObject)
{
try
{
request=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
request=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
request=false;
}
}
}
return request;
}

function updateEvent()
{
window.alert("who's calling ("+this.myState+")");
}
function GetIt(xmlHttp,url)
{
if(xmlHttp)
{
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=updateEvent;
xmlHttp.send(null);
}
}
var xhr1=new getHttpRequest();
xhr1.m

updateEvent()中this代表的是方法本身,this.myState代表updateEvent()中的myState参数,但是在这个方法里你并未定义。
<input type="text" onclick="a(this.value)" />中的this代表的是文本框本身,所以value属性是默认的,不需要写上去。
纯属个人看法。。

this是局部变量,不是全局变量
用传参吧function updateEvent(参数)

1首先:var xhr1=new getHttpRequest();
这里不应该有new
2其次: xhr1.myState="xhr1";
AJAX 里的那个对象XMLHttpRequest没有myState这个属性,你想要的可能是这个属性status,表示就绪状态