Javascript:http请求连接超时

问题描述 投票:0回答:1

最近我一直在Search Console中处理我网站上的一些问题。我的网站似乎在多次抓取时返回超时,禁止Google抓取该网页。我一直想弄清楚原因是什么,但后来我记得我前段时间实现了一些新的javascript,执行http请求来评估图像src是否已经死亡,如果有的话用一个新的src替换它网址。

EG

$(".productimage img").each( function() {
    var image_url2 = $(this).attr("src");
    var http = new XMLHttpRequest();

    http.open("HEAD", image_url2, false);
    http.send();

    if(http.status == "404") {
      var newimage_url2 = "https://example.com/img/newimage.png";
      $(this).attr("src",newimage_url2)
    }
});

我想知道这样的http请求是否会使服务器紧张并且可能导致连接超时错误?

很想听听你的想法和经历。

照顾自己!

javascript http google-webmaster-tools
1个回答
0
投票

在上面的代码中,我看到错过的部分 - 发送请求后无法立即检查状态,您应该先等待回答

  $(".productimage img").each( function() {
  var image_url2 = $(this).attr("src");
  var http = new XMLHttpRequest();

  http.open("HEAD", image_url2, false);
 http.send();

xhr.onreadystatechange = function () {
    if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 404) {
        var newimage_url2 = "https://example.com/img/newimage.png";
  $(this).attr("src",newimage_url2)
    };
  };
 })

很难说,它如何影响爬行,但试试看

© www.soinside.com 2019 - 2024. All rights reserved.