You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用jQuery .load()方法加载外部HTML到空div无内容显示的问题

使用jQuery .load()方法加载外部HTML到空div无内容显示的问题

看起来你遇到的核心问题是:虽然控制台提示加载成功,但#header div里就是看不到header.html的内容。我帮你排查了下代码,问题出在你的Node.js服务器没有处理header.html的请求路由,导致浏览器其实没真正拿到这个文件的内容,虽然jQuery的load回调返回了success,但实际是拿到了空响应或者404页面,所以页面上没东西。

下面是具体的修复步骤:

1. 修复Node.js服务器的路由问题

你的server.js里只处理了//homepage.html/jquery.js的请求,但完全没处理/header.html的请求!当浏览器通过jQuery的load请求这个文件时,服务器返回的是404,但jQuery的load在某些情况下(比如服务器返回HTML格式的404页面)会误判为success。

修改后的server.js需要加上/header.html的路由,另外最好也提前加上/logIn.html的路由(因为你header里的按钮要加载这个文件):

const http = require("http");
const fs = require('fs');
const host = "localhost";
const port = 8060;

const server = http.createServer((req, res) => {
  if (req.method === "GET") {
    // 处理首页请求
    if (req.url === '/' || req.url === '/homepage.html') {
      fs.readFile("homepage.html", function (error, content) {
        if (error) {
          res.writeHead(404, {'Content-Type': 'text/html'});
          res.end("404 Not Found");
        } else {
          res.writeHead(200, {'Content-Type': 'text/html'});
          res.end(content);
        }
      });
    } 
    // 处理jQuery文件请求
    else if (req.url === "/jquery.js") {
      fs.readFile("jquery.js", function (error, content) {
        if (error) {
          res.writeHead(404, {'Content-Type': 'application/javascript'});
          res.end("404 Not Found");
        } else {
          res.writeHead(200, {'Content-Type': 'application/javascript'});
          res.end(content);
        }
      });
    }
    // 新增:处理header.html的请求
    else if (req.url === "/header.html") {
      fs.readFile("header.html", function (error, content) {
        if (error) {
          res.writeHead(404, {'Content-Type': 'text/html'});
          res.end("404 Not Found");
        } else {
          res.writeHead(200, {'Content-Type': 'text/html'});
          res.end(content);
        }
      });
    }
    // 新增:处理logIn.html的请求
    else if (req.url === "/logIn.html") {
      fs.readFile("logIn.html", function (error, content) {
        if (error) {
          res.writeHead(404, {'Content-Type': 'text/html'});
          res.end("404 Not Found");
        } else {
          res.writeHead(200, {'Content-Type': 'text/html'});
          res.end(content);
        }
      });
    }
    // 处理其他未匹配的请求
    else {
      res.writeHead(404, {'Content-Type': 'text/html'});
      res.end("404 Not Found");
    }
  }
});

server.listen(port, host, () => {
  console.log(`Server running at http://${host}:${port}/`);
  console.log(`Displaying content of homepage.html`);
});

2. 优化jQuery的load回调的错误判断

你可以优化一下回调函数,直接打印响应内容,这样能快速排查是否真的拿到了目标文件:

修改homepage.html里的script部分:

$(document).ready(function () {
  function f(responseTxt, statusTxt, xhr) {
    if (statusTxt == "success") {
      console.log("External content loaded successfully!");
      // 新增:打印实际拿到的响应内容,确认是否正确
      console.log("Response content:", responseTxt);
    }
    if (statusTxt == "error") {
      console.log("Error: " + xhr.status + ": " + xhr.statusText);
      // 新增:打印错误的响应内容
      console.log("Error response:", responseTxt);
    }
  }
  $("#header").load("header.html", function (responseTxt, statusTxt, xhr) {
    f(responseTxt, statusTxt, xhr);
  });
});

这样如果服务器返回的是空或者404内容,你能在控制台直接看到,快速定位问题。

3. 额外注意事项

  • 确保header.htmllogIn.htmljquery.js都和server.jshomepage.html在同一个文件夹下,或者调整fs.readFile的路径为正确的相对路径。
  • 测试的时候,一定要通过http://localhost:8060访问,不要直接双击打开homepage.html(直接打开是file协议,会触发跨域限制,jQuery的load会失败)。

按照上面的修改,你的header.html内容应该就能正常加载到#header div里了,点击“Se connecter”按钮也能正常加载logIn.html的内容到#main里。

火山引擎 最新活动