在我添加条件状态属性之前,我的 Ajax 工作得很好。
这是片段
if (XMLHttp.readyState==4 && XMLHttp.status==200){
// do something
}
这是完整的代码
function getXMLHttp()
{
try
{
var xmlhttp = new XMLHttpRequest();
// document.getElementById("Content").innerHTML="<h1>Using XMLHttpRequest Object</h1>";
//alert('Mozilla XMLHttpRequest Obeject Created Successfully');
}
catch(err1)
{
var ieXmlHttpVersions = new Array();
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";
var i;
for (i=0; i < ieXmlHttpVersions.length; i++)
{
try
{
var xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);
// var catatan = "<h1>Using " + ieXmlHttpVersions[i] + "</h1>";
break;
}
catch (err2)
{
var xmlhttp = null;
//alert(ieXmlHttpVersions[i] + " not supported.");
}
}
}
if (typeof xmlhttp == "undefined" || xmlhttp == null){
//document.getElementById("Content").innerHTML="<h1>XMLHttp cannot be created!</h1>";
alert('XMLHttp Request Object Is not Supported Somehow');
}
return xmlhttp;
}
var XMLHttp = getXMLHttp();
function loadData(url, targetID){
if(!url) {var url="data.txt";}
if(!targetID){var targetID='ajaxID';}
XMLHttp.onreadystatechange = function (){getResponse(targetID)};
XMLHttp.open('GET', url, true);
XMLHttp.send(null);
}
function getResponse(targetID){
var data = XMLHttp.responseText;
var ajaxContent=document.getElementById('ajax_content');
if(XMLHttp.readyState == 4){
// This works Just fine, data from data.txt actually fetched
// BUT When i Add this if statement with " && XMLHttp.status==200" It's not returning data from data.txt
if(data.length > 0){
fill(targetID,data);
}
}
}
function fill(ID,data){
hideLoading();
document.getElementById(ID).innerHTML = data;
}
function showLoading(){
document.getElementById('loading').style.visibility='';
document.getElementById('loading_text').innerHTML = '....Loading Please Wait....';
}
function hideLoading(){
document.getElementById('loading').style.visibility = 'hidden';
document.getElementById('loading_text').innerHTML = '';
}
data.txt
我的问题是为什么我在添加&& XMLHttp.status==200
语句时无法获取数据?