加载位于根目录之外的 json 和映像

Loading json and images situated outside root directory

本文关键字:json 映像 根目录 加载      更新时间:2023-09-26

我正在尝试加载一个json文件和一些图像,这些图像位于我的根目录之前的其他目录中。我现在正在尝试使用此代码来获取它。它工作正常,但似乎这不是完美的方法。

$.getJSON("http://localhost/folder1/folder2/folder3/content/LB/contents/content.json", function(json){
    console.log(json);
}

和图像

<img src="http://localhost/folder1/folder2/folder3/content/LB/contents/locations/locationname/thumbnail.png />

我正在尝试将 json 和图像加载到

http://localhost/folder1/folder2/folder3/content/LB/appdrive/html/someHTML.html
http://localhost/folder1/folder2/folder3/content/LB/appdrive/html/js/someJS.js

我只使用HTML,CSS和JAVASCRIPT。所以主要问题是有没有办法将 json 和图像的来源更改为这样的东西

$.getJSON("folder2/folder3/content/LB/contents/content.json", function(json){
    console.log(json);
}

和图像

<img src="../folder2/folder3/content/LB/contents/locations/locationname/thumbnail.png />

还有一件事要说,FOLDER2是根目录。

是的。您可以将您的URL"根"到网站的根目录(例如,"/folder1/folder2/folder3/.../content.json")。请注意前导斜杠。