function CreateXMLHttp()
{
var xmlHttp = null;
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}function PostData()
{
var xmlHttp = new CreateXMLHttp();
if (xmlHttp!=null)
{
xmlHttp.open("get",Url,true);
xmlHttp.onreadystatechange = function(){XmlSub(xmlHttp)};
xmlHttp.send(null);
xmlHttp=null;
}
}
function XmlSub(xmlHttp)
{
//window.alert(xmlHttp.readyState);
if (xmlHttp.readyState==4)
{
if (xmlHttp.status==200 || xmlHttp.status==0)
{
var ChangeStr = xmlHttp.responseText;
window.alert(ChangeStr);
}
}
}老是提示 readyState为空或者不是对象
---->
xmlHttp.onreadystatechange = XmlSub(xmlHttp);
{
alert(xmlHttp)//你加这句,分别在两种写法下测试一下就知道了。
如果照这么写xmlHttp.onreadystatechange = XmlSub(xmlHttp); XmlSub根本不会调用 已经测试
{
if(xmlHttp.readyState==4){}
}
我那样写,回调函数要
function XmlSub1(xmlHttp){
return function(){
if (xmlHttp.readyState==4) {
if (xmlHttp.status==200 || xmlHttp.status==0){
var ChangeStr = xmlHttp.responseText;
window.alert(ChangeStr);
}
} }
}
这样还是提示错误 为指定的错误 也不能得到返回值(但URL已经提交 确定)
而且readyState==4是指HTTP状态改变 在状态未改变之前怎么可以直接获取HTTP状态呢?
xmlHttp=null; //请去掉这行
10楼说的对,
xmlHttp.send(null);
xmlHttp=null;应该写在回调函数里if (xmlHttp.status==200 || xmlHttp.status==0){
var ChangeStr = xmlHttp.responseText;
window.alert(ChangeStr);
xmlHttp=null;
}