可以在网页中更新JSON数据吗?

huangapple go评论59阅读模式
英文:

Is it possible to update JSON data in a webpage?

问题

我按下网页上的按钮执行addEventListener()函数。之后,该函数更改一些JSON数据并保存它。然后,在将来,人们可以访问JSON数据。

一个示例是使用Netlify部署的登录/注册页面。

所以我首先有一个简单的文件系统:

login-register-project:
    - index.html
    - db.json
    - login-page:
        - login.html
        - login.js
    - register-page:
        - register.html
        - register.js
    - home-page:
        - hub.html
        - hub.js

db.json中包含:

{
    "username":{
        "pw":"password",
        "login_bool":false
    }
}

home-page是登录或注册完成后显示的页面。

index.html将用户发送到register-pageregister.html。然后,其中将有一个登录选项,用户可以使用它来选择注册或登录。

在填写register.html的注册详细信息并单击"登录"按钮后,register.js将数据放入db.json,将login_bool设置为true,并将用户重定向到https://websitename.com/home-page/hub.html?{user的用户名}|{user的密码}

或者,在填写login.html的登录详细信息并单击"登录"按钮后,login.jslogin_bool设置为true,并将用户重定向到https://websitename.com/home-page/hub.html?{user的用户名}|{user的密码}

然后,hub.html检查给定的用户名和密码数据是否正确,并检查login_bool是否设置为true。如果所有信息都正确,网站将显示其内容。如果login_bool设置为false或用户名/密码不正确,用户将被重定向到index.html

顺便说一下,在hub.html中将有一个"登出"按钮,允许用户"登出"并将login_bool设置为false

现在,如果这样运作,那么如何从部署的JSON文件中检索数据并将其复制到我的JSON文件中,以便在部署新更新时不会丢失数据?

如果我漏掉了什么/我的信息不正确/您需要进一步的信息,请告诉我。

谢谢回答的任何人。

英文:

My meaning is I press a button (in the webpage) which executes an addEventListener() function. After that, the function changes some JSON data and saves that. Then, in the future, people could access the JSON data.

An example is a login/register page deployed using Netlify.

So I first have a simple file system:

login-register-project:
    - index.html
    - db.json
    - login-page:
        - login.html
        - login.js
    - register-page:
        - register.html
        - register.js
    - home-page:
        - hub.html
        - hub.js

Inside db.json has:

{
    "username":{
        "pw":"password",
        "login_bool":false
    }
}

home-page is the page displayed after login or register is complete.

index.html sends the user to register-page's register.html. There would then be a login instead option in there, which the user can use to choose either to register or login.

After filling in register.html's register details and clicking the "sign in" button, register.js would then put the data into db.json, sets login_bool to true and redirect the user to https://websitename.com/home-page/hub.html?{user's usrname}|{user's pw}

Alternatively, after filling in login.html's login details and clicking the "login" button, login.js would then set login_bool to true and redirect the user to https://websitename.com/home-page/hub.html?{user's usrname}|{user's pw}.

hub.html then checks whether the username and password data given were correct and checks if login_bool was set to true. If all information is correct, the website displays its contents. If login_bool was set to false or the username/password were incorrect, the user will be redirected to index.html

BTW, there will be a "log out" button in hub.html that lets users to "log out" and sets login_bool to false.

Now, if this works, then how would I retrieve the data from the JSON file deployed and copy it to my JSON file so that when I deploy a new update, no data will be lost?

If I missed anything/my information was incorreect/you require furthur information, please tell me.

Thanks to anyone who answers.

答案1

得分: 0

短回答:您的逻辑是正确的,但这不是Netlify的工作方式。我建议使用外部数据库来存储这些数据。

长回答:Netlify的部署一旦发布就是不可变的。这意味着,您希望推送到站点代码的任何更改都必须触发新的部署并上传这些更改。虽然您可能可以使用Netlify API 来实时更新站点(比如在点击按钮时),但这对于您尝试实现的任务来说会过于繁琐,更不用说有点困难。流程将如下:

  1. 获取当前部署中文件的列表
  2. 将您希望上传的新文件的哈希值计算为SHA1
  3. 创建一个新的部署,包括先前文件的列表+您希望上传的新文件
  4. 上传新文件
  5. 等待几秒钟,使部署生效
  6. 从更新后的站点获取数据

即使您设法完成所有这些步骤,这将明显更慢,而且在并发读/写的情况下难以管理。

英文:

Short answer: Your logic is correct but that's not how it works on Netlify. I'd rather advise using an external database to store this data.

Long answer: Netlify's deploys are immutable once published. This means that, any change that you wish to push to your site's code, would have to trigger a new deploy and upload those changes. While you can possibly use Netlify API to handle updating your site on-the-fly (like on a click of the button), this would be an overkill for the task you're trying to achieve, not to mention a bit difficult. The flow would look something like:

  1. Fetch the list of files in the current deploy
  2. Hash the new files that you wish to upload as SHA1
  3. Create a new deploy with the list of previous files + new file that you wish to upload
  4. Upload the new file
  5. Wait a few seconds for the deploy to go live
  6. Fetch the data from the updated site

Even if you manage to do all this, this would definitely be much slower, not to mention difficult to manage in case of concurrent read/writes.

huangapple
  • 本文由 发表于 2023年2月16日 17:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定