|
楼主 |
发表于 2014-12-3 14:58:35
|
显示全部楼层
代码是完全参照上面写的呀,为什么还是:
alert("xmlHttp.readyState = " + xmlHttp.readyState); 显示1
alert("xmlHttp.status = " + xmlHttp.status); 显示0
呢??
代码如下:
--------------
var xmlHttp;
//创建xmlHttp
Model.prototype.createXMLHttpRequest = function()
{
xmlHttp = null;
if(window.ActiveXObject)
{
// code for IE5 and IE6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
// code for all new browsers
xmlHttp=new XMLHttpRequest();
}
//alert("xmlHttp="+xmlHttp);
};
//拼出要发送的用户和密码数据
Model.prototype.createQueryString = function()
{
//取得用户名和密码
var userid = this.getElementByXid("userid").value;
var passwd = this.getElementByXid("passwd").value;
//拼出要发送的用户和密码数据
var queryString="firstName=" + userid + "&middleName=" + passwd + "&birthday=1111" ;
return queryString;
};
//使用get方式发送
Model.prototype.doRequestUsingGET = function()
{
this.createXMLHttpRequest();
var queryString="./GetAndPostExample?";
queryString=queryString+ this.createQueryString() + "&timeStamp=" + new Date().getTime();
if (xmlHttp !== null){
//onreadystatechange 是一个事件句柄。
//它的值(handleStateChange)是一个函数的名称,当 XMLHttpRequest 对象的状态发生改变时,会触发此函数。
//状态从 0 (uninitialized) 到 4 (complete) 进行变化。
//仅在状态为 4 时,我们才执行代码。
xmlHttp.onreadystatechange= this.handleStateChange();
xmlHttp.open("GET",queryString,true);
xmlHttp.send(null);
}else{
alert("Your browser does not support XMLHTTP.");
}
};
//使用post方式发送
Model.prototype.doRequestUsingPost = function()
{
this.createXMLHttpRequest();
var url="./GetAndPostExample?timeStamp=" + new Date().getTime();
var queryString= this.createQueryString();
xmlHttp.open("POST",url,true);
//onreadystatechange 是一个事件句柄。
//它的值(handleStateChange)是一个函数的名称,当 XMLHttpRequest 对象的状态发生改变时,会触发此函数。
//状态从 0 (uninitialized) 到 4 (complete) 进行变化。
//仅在状态为 4 时,我们才执行代码。
var urlqrystr = url + "&" + queryString;
xmlHttp.onreadystatechange= this.handleStateChange();
xmlHttp.setRequestHeader("Content-Length",urlqrystr.length);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(queryString);
};
Model.prototype.handleStateChange = function()
{
alert("xmlHttp.readyState = " + xmlHttp.readyState);
alert("xmlHttp.status = " + xmlHttp.status);
if(xmlHttp.readyState==4) // 4 = "loaded"
{
if(xmlHttp.status==200) // 200 = OK
{
this.parseResults();
}
else
{
alert("Problem retrieving XML data");
}
}
};
//解析返回值
Model.prototype.parseResults = function()
{
//var responseDiv= this.getElementByXid("output1");
//if(responseDiv.hasChildNodes())
//{
// responseDiv.removeChild(responseDiv.childNodes[0]);
//}
var dzb = eval("(" + xmlHttp.responseText +")");
alert(dzb);
alert("后台返回的返回值: "+xmlHttp.responseText);
//var responseText=document.createTextNode(xmlHttp.responseText);
//responseDiv.appendChild(responseText);
};
|
|