代码之家  ›  专栏  ›  技术社区  ›  swabygw

XMLHttpRequest在新服务器上失败

  •  0
  • swabygw  · 技术社区  · 7 年前

    在我的localhost IIS服务器上,下面的Javascript函数可以完美地工作。当我将代码迁移到我的其他IIS服务器(它是一个Windows VPS)时xmlhttp.status文件总是返回404(即使它检查的文件在那里)。

    function startInterval(result) {
      //var fname = "http://localhost/excelfiles/Ad_Activity_1_145.csv";
      var path = "http://<% =Request.Url.Host %>/excelfiles/"; 
      var fname = path + a.substring(0,a.length-1) + "_Activity_" + c + '_' + result + '.csv';
      var checkCounter = 0;
      checkInterval = setInterval(function() {
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.open('HEAD', fname);
      xmlhttp.onreadystatechange = handleHttpResponse_check;
      xmlhttp.send(null);
    
      function handleHttpResponse_check()
      {
        if(xmlhttp.readyState == 4){
          if (xmlhttp.status == 200) {
            //ctrl.innerHTML = '<a onclick="goToURL(\''+fname+'\');return false;">Open file!</a>';
            ctrl.innerHTML = '<a onclick="goToURL(\''+fname+'\');return false;"><img src="images\\Excel_Icon.jpg" style="width:20px;height:20px;cursor:pointer;"/></a>';
            clearInterval(checkInterval);
            } else if (xmlhttp.status == 404) {
              checkCounter += 1;
              if(checkCounter >= 5){
              clearInterval(checkInterval);
              ctrl.innerHTML = 'ERROR: File not created';
              }                                                                                                                 
            }
          }
        }
    
      }, 1000);
    }
    

    我怀疑XMLHttpRequest在新服务器上失败了。fname变量具有正确的路径/文件名。handleHttpResponse\u check函数正在正确执行…仅此而已xmlhttp.status文件总是在新服务器上返回404,尽管文件位于路径/文件名处。localhost服务器完美地检测到文件xmlhttp.status文件在服务器上返回200。你知道新服务器的情况吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Adam Chubbuck    7 年前

    404状态码表示服务器没有找到任何与请求URI匹配的内容。检查请求的资源的路径,以确保生成的路径是预期路径,并查看是否可以访问该资源。