我正在用AJAX获取正在用javascript解析的二进制文件(Quake 2 BSPs,如果有人关心的话。)获取和解析初始文件的代码运行良好,大致如下所示:
function loadFile(url) {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
var parsed = parseFile(request.responseText);
}
};
request.open('GET', url, true);
request.overrideMimeType('text/plain; charset=x-user-defined');
request.setRequestHeader('Content-Type', 'text/plain');
request.send(null);
}
正如我所说的,这很好,所有内容都正确加载和解析。但是,该文件还描述了几个需要检索的辅助文件(纹理),因此我添加了一个内部循环,该循环应加载和解析所有这些文件,如下所示:
function loadFile(url) {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
var parsed = parseFile(request.responseText);
for(var i = 0; i < parsed.files.length; ++i) {
loadSecondaryFile(parsed.files[i].url); // Request code here is identical to this function
}
}
};
request.open('GET', url, true);
request.overrideMimeType('text/plain; charset=x-user-defined');
request.setRequestHeader('Content-Type', 'text/plain');
request.send(null);
}
function loadSecondaryFile(url) {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
var parsed = parseSecondaryFile(request.responseText);
}
};
request.open('GET', url, true);
request.overrideMimeType('text/plain; charset=x-user-defined');
request.setRequestHeader('Content-Type', 'text/plain');
request.send(null);
}
但是从该循环中发出的每个请求都会立即失败,并显示以下消息(在Chrome、Dev Channel中):
网络错误:XMLHttpRequest异常101
loadSecondaryFile
外部
loadFile
onreadystatechage
另一种可能是糟糕的juju,但是将辅助ajax调用包装在
setTimer
有什么想法吗?