我更喜欢不使用 jQuery(据我了解,该文件相对较大,每次加载页面时都会“吃掉”很多带宽)。我检查了我的代码(如下),XMLHttpRequest 编写正确,并且不是跨域的。在 Chrome 上工作正常,但在 IE 上,如果只有一个请求正在运行,那么没问题,但下面的代码使用 2 个 XMLHttpRequest 实例和 BOTH 以某种方式访问我的 CGI 但总是返回空字符串。我也尝试过 send(null)、send("") 或 send('')。
我的代码:
<script type="text/javascript">
var SMR;
var UPR;
var F;
var G;
function MUPD() {
F = window.setInterval(Updator, 5000);
G = window.setInterval(Listener, 5000);
if(typeof XMLHttpRequest !== 'undefined') { SMR = new XMLHttpRequest(); UPR = new XMLHttpRequest(); }
else {
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"]
for(var i = 0, len = versions.length; i < len; i++) {
try {
SMR = new ActiveXObject(versions[i]);
UPR = new ActiveXObject(versions[i]);
break;
}
catch(e){}
}
}
}
function Updator() {
if (document.cookie.length > 0) {
var begin = document.cookie.indexOf("TCPID=");
if (begin != -1) {
var end = document.cookie.indexOf(";", begin+6);
if (end == -1) end = document.cookie.length;
var fupd=UPD(unescape(document.cookie.substring(begin+6, end))).split("-");
if(fupd[0]=="OK") {
for(var i=1 ; i<fupd.length ; i+=2) {
var elm=document.getElementByID("I"+fupd[i]);
elm.setAttribute("src", "/theclub/cgi-bin/personalimage.cgi?UID="+fupd[i]);
if(fupd[i+1].toString()!=="OFF") { document.getElementById("T"+fupd[i]).innerHTML=fupd[i+1]; }
}
}
}
else { window.top.location.href="/theclub/login.htm?MOD=NEW"; }
}
}
function UPD(cvl) {
try {
UPR.open("GET", "/theclub/cgi-bin/chkpubupd.cgi?CVL="+cvl, false);
UPR.setRequestHeader("Content-type", "text/x-www-form-urlencoded");
UPR.send('');
if (UPR.status == 200) { return UPR.responseText; }
else { alert("Error: "+UPR.status+" >> "+UPR.responseText); return ""; }
}
catch (e) { alert("Error in updating system"); return ""; }
}
function PWR(cvl) {
try {
SMR.open("GET", "/theclub/cgi-bin/getmessages.cgi?CVL="+cvl, false);
SMR.send('');
if (SMR.status == 200 && SMR.responseText!=="") { alert(SMR.responseText);}
else if(SMR.status != 200) { alert(SMR.status+" >> "+SMR.responseText); }
}
catch (e) { alert("Error receiving messages"); }
}
function Listener() {
if (document.cookie.length > 0) {
var begin = document.cookie.indexOf("TCPID=");
if (begin != -1) {
var end = document.cookie.indexOf(";", begin+6);
if (end == -1) end = document.cookie.length;
PWR(unescape(document.cookie.substring(begin+6, end)));
}
else { window.top.location.href="/theclub/login.htm?MOD=NEW"; }
}
}
...
</script>