无法使用jQuery将html文件加载到另一个html文件

时间:2021-04-20 00:12:44

I'm getting a 404 file not found when I try to include an html file into another html using jQuery. Here's my code inside my index.html

当我尝试使用jQuery将html文件包含到另一个html中时,我找不到404文件。这是我的index.html中的代码

<script> 
    $(function(){
      $("#portfolio").load("list.html"); 
    });
</script> 

The way my file directory is setup is that both index.html and list.html are within a views folder and being served via a node.js server.

我的文件目录的设置方式是index.html和list.html都在一个views文件夹中,并通过node.js服务器提供服务。

1 个解决方案

#1


2  

The docs explains that you may use a callback parameter on the load method, which gives your information about the load request.

文档解释了您可以在load方法上使用回调参数,该方法提供有关加载请求的信息。

$( "#portfolio" ).load( "/list.html", function( response, status, xhr ) {
  if ( status == "error" ) {
    var msg = "Sorry but there was an error: ";
    $( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
  }
});

Also, you may check the Network tab of your browser´s Developer Tools to get more information, including URL that your script tried to reach. Debug from there.

此外,您可以查看浏览器的开发人员工具的“网络”标签,以获取更多信息,包括脚本尝试访问的网址。从那里调试。

#1


2  

The docs explains that you may use a callback parameter on the load method, which gives your information about the load request.

文档解释了您可以在load方法上使用回调参数,该方法提供有关加载请求的信息。

$( "#portfolio" ).load( "/list.html", function( response, status, xhr ) {
  if ( status == "error" ) {
    var msg = "Sorry but there was an error: ";
    $( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
  }
});

Also, you may check the Network tab of your browser´s Developer Tools to get more information, including URL that your script tried to reach. Debug from there.

此外,您可以查看浏览器的开发人员工具的“网络”标签,以获取更多信息,包括脚本尝试访问的网址。从那里调试。