1
下面的顺序调用Ajax来读取HTTP响应的报头中的值,并设置一个变量称为_ecpop和使用,对于今后的业务程序...AJAX延迟扰乱操作
所以我定义了一个全局变量来叫_ecpop作为
var _ecpop="";
,然后这里是Ajax调用代码:
if (typeof XMLHttpRequest != "undefined") {
_ec_req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
_ec_req = new ActiveXObject("Microsoft.XMLHTTP");
}
// make the request
_ec_req.open("GET", requestString, true);
// function to deal with response
_ec_req.onreadystatechange = function() {
if (_ec_req.readyState==4)
{
if (_ec_req.status==200)
{
var respHeaders = _ec_req.getAllResponseHeaders();
// strip out unnecessary headers
respHeaders = respHeaders.replace (/\n/g,"|X|");
var beg = respHeaders.indexOf("ECAcc (");
_ecpop = respHeaders.substring(beg+7,beg+10);
//make an if statement here...
console.log(_ecpop);
}
}
}
console.log(_ecpop);
和代码的其余部分使用_ecpop。
问题是浏览器使用_ecpop =“”执行其余代码。即第一个第二个console.log(_ecpop);返回空然后第一个console.log(_ecpop);返回正确的值。
有没有人知道我可以绕过它?它太刺激...
谢谢,
埃米尔。
仍然无法正常工作..至少在Firefox上 – Amir
请确保您清除了浏览器的缓存,以确保您使用的是最新版本的脚本。如果问题仍然存在,请在Firebug控制台中检查网络请求后发布有关该问题的更多信息。 –
确定这里是问题...我需要调用console.log(_ecpop)(之后)...之前调用_ec_req.send(null):-) – Amir