【问题标题】:What is different about xmlhttprequest in FirefoxFirefox 中的 xmlhttprequest 有什么不同
【发布时间】:2018-10-26 17:12:30
【问题描述】:

我的代码在 Chrome 和 Safari 中运行,但在 FF 中挂起。

我删除了不必要的代码部分。

我使用控制台命令来显示第一个循环有多远,它会在 xhr 打开和发送命令之前执行第二个日志。

如果打开/发送命令存在,则循环只发生一次,如果我删除打开/发送命令,则循环成功完成。

目前正在使用 FF 62nightly,但自从 Quantum 推出以来,这个问题一直困扰着我,我现在正试图弄清楚为什么它不能正常工作。

for (i = 0; i < length; i++) {
  (function(i) {
    // new XMLHttpRequest
    xhr[i] = new XMLHttpRequest();
    // gets machine url from href tag
    url = rows[i].getElementsByTagName("td")[0].getElementsByTagName('a')[0].getAttribute('href');
    // Insert the desired values at the end of each row; 
    // will try to make this customizable later as well


    insertVNC[i] = rows[i].insertCell(-1);
    insertSerial[i] = rows[i].insertCell(-1);
    insertVersion[i] = rows[i].insertCell(-1);
    insertFreeDiskSpace[i] = rows[i].insertCell(-1);

    // the fun part: this function takes each url, loads it in the background, 
    // retrieves the values needed, and then discards the page once the function is complete; 
    // In theory you could add whatever you want without taking significantly longer 
    // as long as it's on this page
    console.log(i);

    xhr[i].onreadystatechange = function() {
      if (xhr[i].readyState == 4 && xhr[i].status == 200) {
      }
    };

    //"Get" the "Url"... true means asyncrhonous
    console.log(url);
    xhr[i].open("GET", url, true);
    xhr[i].send(null);

  })(i); //end for loop

}

【问题讨论】:

  • i 大概有多大?例如,如果您一次尝试 100 个连接,则不同的浏览器可能有不同的限制。 length 只有 1 或 2 时还会发生这种情况吗?
  • 最多说 40。是的,当 i 为 1 或 2 时它会这样做
  • 还建议使用调试器单步执行。您的 Web 控制台中是否有任何错误?
  • 这是一个在 FF 60.0 中适合我的小提琴:jsfiddle.net/khrismuc/rfm5fcw1
  • 这是一个扩展的事实可能与潜在的解决方案密切相关。如果是这样,您可能希望适当地标记您的问题。

标签: javascript firefox xmlhttprequest


【解决方案1】:

我无法告诉你为什么它会在 Firefox 中出现问题。我不相信从任何浏览器发送任意多的请求

我会亲自尝试这个,因为它不会在一个完成之前触发下一个

const urls = [...document.querySelectorAll("tr>td:nth-child(0) a")].map(x => x.href);
let cnt=0;
function getUrl() {
  console.log(urls[cnt]);
  xhr[i].open("GET", urls[cnt], true);
  xhr[i].send(null);
}

let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (xhr[i].readyState == 4 && xhr[i].status == 200) {
    if (cnt>urls.length) getUrl();
    cnt++;
  }
}
getUrl();

【讨论】:

  • 不是我,但我上面提供的解决方案在除 Firefox 之外的所有其他浏览器中都可以正常工作,所以它必须是 xhr 的东西,但我不确定是什么。
  • 我明白,不过运行你的脚本我会觉得不安全 ;)
  • 为什么会这样,它只加载网页而不是你可以访问我正在使用的网站
  • 从“它会真正运行到最后”的方式安全 - 如果你有 1000 个 url,它会敲击服务器,正如你所看到的,Firefox 甚至不想发送那么多请求同时
猜你喜欢
  • 2011-04-18
  • 2018-12-08
  • 1970-01-01
  • 2014-02-21
  • 2010-12-22
  • 2020-01-21
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
相关资源
最近更新 更多