Javascript 未加载到 XMLHTTPRequest

问题描述

为了清楚起见,以下是我的“网站”的运作方式:

  1. 我加载了一个 HTML 文件 (index.html)
  2. Javascript (index.js) 检查用户是否通过身份验证(此处不一定相关)
  3. Javascript (index.js) 将 XMLHttpRequest 发送到服务器上的 PHP 文件,该文件检查身份验证,然后返回要包含在 index.html 文件中的 HTML 文件 (result.html)。

我的问题是返回的 HTML 文件/内容 (result.html) 需要一个单独的 Javascript 文件(我们称之为 result.js),但它似乎没有在加载 result.html 时加载/回来。正在加载 CSS 文件

我该如何规避这个问题?还是我只是做错了什么? 希望这个问题足够清楚!

这里是提到的文件

index.html:

    <!DOCTYPE html>
    <html lang="de">
    <head>
      <script src="js/index.js"></script>
      <link rel="stylesheet" href="css/index.css">
    </head>
    <body onload="initDoc()">
      <div id "content-loader">
      </div>
    </body>
  </html>

index.js:

function initDoc(){
    document.getElementById("content-loader").innerHTML = null;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4) {
            if (this.status == 200) {
                document.getElementById("content-loader").innerHTML = this.responseText;
            }
            if (this.status == 404) {document.getElementById("content-loader").innerHTML = "Doc not found";}
        }
    }
    xhttp.open("GET","/PHP/authentication.PHP?var=" + "examplevar");
    xhttp.send();
}

result.html:

    <!DOCTYPE html>
    <html lang="de">
    <head>
      <script src="js/result.js"></script>
      <link rel="stylesheet" href="css/result.css">
    </head>
    <body>
     Content here
    </body>
  </html>

result.js 并不重要,因为它没有被加载..

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)