简体   繁体   English

我可以连接到服务器上的本地 .json 文件并对其进行模板化吗?

[英]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.

相关问题 为什么我的 android 模拟器无法连接到我的本地服务器? - Why can't I get my android emulator to connect to my local server? 我无法连接到本地主机上的服务器 - I Can't connect to my server on localhost 如何使用Redux访问本地JSON文件以在整个React应用程序中使用? - How can I access a local JSON file with Redux to use throughout my React app? 如何将表单中的数据发布到本地 JSON 文件? - How can I POST data from my form to a local JSON file? 如何使用Javascript覆盖我的甾体js应用程序中的本地JSON文件? - How can I overwrite the local JSON file in my steroids js application with Javascript? 如何构造我的MustacheJS模板,以基于JSON文件中的值添加动态类? - How can I structure my MustacheJS template to add dynamic classes based on the values from a JSON file? 如何从本地WAMP服务器上的www解析一个简单的xml文件? - How can I parse a simple xml file from the www on my local WAMP server? Bootstrap - 我的表无法读取本地 JSON 文件 - Bootstrap - My table can't read a local JSON file 为什么我的 JSON post 方法不适用于本地 Web 服务器中的 JSON 文件? - Why isn't my JSON post method working on my JSON file in local web server? 在本地服务器上编辑JSON文件 - Edit JSON file on local server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM