xmlhttp 为空 或不是对象,哪错了??

于娜 2008-07-13 10:54:17
<script language="javascript" type="text/javascript">

var xmlhttp;

function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
}

function startCollect()
{
xmlhttp = createXMLHttpRequest();
xmlhttp.open("GET","Index2.aspx",true);
xmlhttp.onreadystatechange = stateChange;
xmlhttp.send(null);

}

function stateChange()
{
if(xmlhttp.readystate==4 && xmlhttp.status==200)
{
var data = xmlhttp.responseText;
document.getElementById("txtName").value = data;
}
}
</script>
...全文
485 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
smarteagle 2011-08-24
  • 打赏
  • 举报
回复
我觉得主要是因为createXMLHttpRequest()函数没有返回值 像你这样使用的话要在这个函数里面return xmlhttp
「已注销」 2008-07-15
  • 打赏
  • 举报
回复
function stateChange()
{
if(xmlhttp.readystate==4 && xmlhttp.status==200)
{
var data = xmlhttp.responseText;
document.getElementById("txtName").value = data;
}
}


javascript是大小写敏感的 回调函数中的判断语句应该改为: xmlhttp.readyState==4 && xmlhttp.Status ==200
holy_coder 2008-07-14
  • 打赏
  • 举报
回复
UP
woshigodwolf 2008-07-14
  • 打赏
  • 举报
回复
改的地方,我都注释了,如下
<script language="javascript" type="text/javascript">

var xmlhttp;

function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
}

function startCollect()
{
createXMLHttpRequest();//这里改了
xmlhttp.open("GET","Index2.aspx",true);
xmlhttp.onreadystatechange = stateChange;
xmlhttp.send(null);

}

function stateChange()
{
if(xmlhttp.readystate==4)
{
if(xmlhttp.status==200 || xmlhttp.status==0) {//0是指在本地测试
var data = xmlhttp.responseText;
document.getElementById("txtName").value = data;
}
}
}
</script>
chenguang79 2008-07-14
  • 打赏
  • 举报
回复
<script language="javascript">
var xmlhttp;
var requestType;
function createXmlHttp()
{
if(window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
}

function isno()
{
createXmlHttp();
var brid = document.getElementById("Text3");
var url = "aaa.aspx?bird="+ brid.value;
xmlhttp.open("GET",url,true);
xmlhttp.onreadystateChange = callback;
xmlhttp.send(null);
}

function callback()
{
if(xmlhttp.readyState==4)
{
var message = xmlhttp.responseText;
//alert(message);
sendMessage(message);
}
}



这是我用的,好用,你对照一下吧
ambitionzfx 2008-07-13
  • 打赏
  • 举报
回复
xmlhttp.readystate应该为xmlhttp.readyState
对大小写敏感
yzlxy 2008-07-13
  • 打赏
  • 举报
回复
可能下面这个函数没有执行到判断语句里
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
}

改成这样

function createXMLHttpRequest()
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
if(xmlhttp ==null)
{
xmlhttp = new XMLHttpRequest();
}
}

52,802

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧