.load() and relative paths

匆匆过客 提交于 2020-01-09 11:09:29

问题


.load() is giving me trouble. I'm working on a section loader project and I just can't seem to fetch the file that I need.

What I am trying to achieve: #sectionContainer is empty on document load, but on document ready it is 'filled' with Pages1.html. This is done by a JavaScript file sections.js. The JS file and the index.html are NOT in the same folder. Here is the site structure (I am running a lot of projects on my site)

  • main folder
    • Project 1
    • Project 2 (sectionLoaderTest/)
      • index.html
      • Pages1.html
      • Pages2.html
      • css/
      • js/
        • sections.js
    • Project 3
    • ...

And the code I use to load Pages1.html on ready:

$(document).ready(function () {
    $("#sectionContainer").load("../Pages1.html", function (response, status, xhr) {
        if (status == "error") {
            var msg = "An error occurred. Status code: ";
            $("#error").html(msg + xhr.status + ". Status text: " + xhr.statusText);
        }
    });
});

I have tried every possible method (/, ./, ., ../, ..) that I know of and nothing seems to work. Here is the test case.

Does anyone know what I am doing wrong?


回答1:


./Pages1.html should work. Tested all accounts for them in the address bar.




回答2:


Your AJAX URLs should be relative to the page you're on, so you want "Pages1.html". What you have in the test case (..Pages1.html) will never work, as that's not a valid reference. (Did you mean to do ../Pages1.html?)



来源:https://stackoverflow.com/questions/10605200/load-and-relative-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!