[英]Can i connect to a local .json file on my server and template it?
I have a Webserver on kasserver.com and with a PHP script I fetch an API every 15 mins and save it on a my webserver.我在kasserver.com上有一个网络服务器,我使用 PHP 脚本每 15 分钟获取一个 API 并将其保存在我的网络服务器上。 Now I want to template the saved JSON data, but I can reach it local.
现在我想对保存的 JSON 数据进行模板化,但我可以在本地访问它。
handleEvents('test, "./json/tester.json");
function handleEvents(id, link) {
fetch(link).then(res => {
res.json().then(
t => {
if (t.length > 0) {
let output = "";
for(let i=0, len = t.length; i < len; i++) {
output = data[i].person;
}
document.getElementById(id).innerHTML = output;
} else {
document.getElementById(id).innerHTML = "<div>Aktuell sind noch keine genaueren Details verfügbar</div>";
}
}
)});
}
But it tries to fetch a local URL instead of using the data from the Webserver.但它尝试获取本地 URL,而不是使用来自 Web 服务器的数据。 I couldn't find how to fetch a local JSON file on my server right yet.
我还找不到如何在我的服务器上获取本地 JSON 文件的方法。
You cannot access server's local location by client.您无法通过客户端访问服务器的本地位置。 If your file is located in public area of PHP server, you can access it by full URL with domain
如果您的文件位于 PHP 服务器的公共区域,您可以通过带域的完整 URL 访问它
function handleEvents(id, link) {
fetch(`https://${location.hostname}${link}`)
.then(res => {
// ...
// ...
}
}
handleEvents('test', '/json/tester.json')
So, you have to write the function like this.所以,你必须像这样编写函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.