【问题标题】:Trouble using AJAX request to include navigation menu使用 AJAX 请求包含导航菜单时遇到问题
【发布时间】:2013-10-28 20:54:52
【问题描述】:

我在 Stack Overflow 上使用 @minitech 编写的 AJAX 请求,虽然它似乎在单个目录中运行良好,但当我尝试从另一个源(如本例中)调用要加载的页面时请求页面,一些脚本(即 jQuery 脚本)从被请求的页面中删除。

我的问题是:有没有办法在请求将文件包含到请求页面之前加载文件?由于工作上的限制,我无法使用 PHP,所以 Java 是首选方法。

这是我目前正在使用的脚本;

<script type="text/javascript">
    var ajaxRq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
    ajaxRq.open("GET", "abcde.html", false);
    ajaxRq.send(null);
    document.write(ajaxRq.responseText);
    </script>

这是@minitech 前段时间提交的;可以找到原文here

~

所以总结一下:这行得通 -

<script type="text/javascript">
    var ajaxRq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
    ajaxRq.open("GET", "first/abcde.html", false);
    ajaxRq.send(null);
    document.write(ajaxRq.responseText);
    </script>

当发出请求的文件位于目录 /first/ 中时

~

这不起作用 -

<script type="text/javascript">
    var ajaxRq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
    ajaxRq.open("GET", "first/abcde.html", false);
    ajaxRq.send(null);
    document.write(ajaxRq.responseText);
    </script>

当发出请求的文件位于目录 /first/second/ 中时

【问题讨论】:

    标签: javascript html ajax get


    【解决方案1】:

    如果您的文件在/first/second,要到达abcde.html,您可以通过两种方式:

    绝对网址:

    /first/abcde.html

    相对网址:

    ../abcde.html

    如果你请求first/abcde.html实际上会尝试在

    /first/second/first/abcde.html 可能不存在。

    根据我们掌握的信息,我能想到的就是这些了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-07
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2014-11-02
      相关资源
      最近更新 更多