从另一个 HTML 文件获取引导模式内容

nur*_*l98 5 html twitter-bootstrap

所以基本上我想要实现的是从另一个 html 文件获取引导模式的内容,而不是将模式内容放在索引 html 文件上(因为我需要多次使用它)。

我尝试从高处和低处寻找答案,最接近的是:Getting Bootstrap's modal content from another page

但是,我有一个问题,因为它没有显示任何模式内容。它只是显示一个空白模式,我不知道我哪里出了问题。

以下是我的相关代码: 主要 HTML:

<div class="panel panel-default">
    <div class="panel-heading">
        <i class="fa fa-bar-chart-o fa-fw"></i> Pre-IDA Test
    </div>

    <div class="panel-body">
        <div id="chartdiv" style="width: 100%; height: 400px;"></div>
        <script> pretest() </script>
        <a href="pretestmodal.html" class="btn btn-info" data-toggle="modal" data-target="#testing">View Data</a>
        <div id="testing" class="modal fade" role="dialog" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                </div>
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

模态内容:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>
    <body>

    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Details</h4>
    </div>
    <!-- /.modal-header -->
    <div class="modal-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-striped">
                <thead>
                    <tr>
                        <th>Year/Quarter</th>
                        <th>Indoor Quarterly Results</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>2016 Q1</td>
                        <td>Q1 (59/60)</td>                
                    </tr>
                    <tr>
                        <td>2016 Q2</td>
                        <td>Q2 (58/60)</td>     
                    </tr>
                </tbody>
            </table>
        </div>
        <!-- /.table-responsive -->
    </div>
    <!-- /.modal-body -->
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
    <!-- /.modal-footer -->
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

编辑: 查看 JavaScript 控制台后,我发现一个错误:

jquery.min.js:4 XMLHttpRequest cannot load file:///E:/PORTAL/pages/pretestmodal.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
Run Code Online (Sandbox Code Playgroud)

知道我该如何解决这个问题吗?

Dan*_*hur 0

您遇到的问题是在 Google Chrome 中使用协议查看网站文件时非常常见的问题file:///

如果您查看日志中收到的错误消息:

仅协议方案支持跨源请求:http、data、chrome、chrome-extension、https。

作为一点见解,当您尝试从另一个域访问资源(例如 Javascript 文件或 CSS 文件)时,就会发生跨源请求。当您将网站视为本地文件(即使用 Chromefile:///协议)时,您可以访问的文件类型是有限的。这称为同源策略。这些策略的存在是为了防止影响您的计算机的攻击,例如跨站点脚本 (XSS) 攻击。

Mozilla开发者网络有一篇关于 URI 同源策略的有趣文章file:///,它很好地解释了您的问题:

例如,如果您有一个文件 foo.html,它访问另一个文件 bar.html,并且您已从文件 index.html 导航到该文件,则仅当 bar.html 与 index.html 位于同一目录中时,加载才会成功或者在与index.html 相同的目录中包含的目录中

将此应用于您遇到的错误,大概您尝试访问的文件位于导致跨源请求错误的不同目录中。

值得注意的是,所有这些问题的发生都是因为您正在本地文件系统上测试您的网站。要解决此问题,您应该尝试在localhost服务器上测试您的网站。这实际上意味着您可以使用以下任一方式测试您的网站HTTPHTTPS

localhost在您的 PC 上创建服务器的一些好的解决方案是:

  • XAMPP - 适用于 Windows、Mac 和 Linux 的良好解决方案
  • WampServer - 另一种仅适用于 Windows 的解决方案。

我不熟悉 WampServer,所以这里是 XAMPP 的说明:

  • 访问 XAMPP 网站并下载适当的安装程序。这将提示您选择安装位置。在 Windows 上,这通常是C:\xampp.
  • 将所有网页文件放入该文件夹中<xampp installation folder>\htdoc\
  • 打开XAMPP控制面板并单击Start单击Apache
  • 在您的网络浏览器中,输入localhost然后按Enter
  • 您现在应该能够查看您的网站,而不会出现您所描述的问题。