【发布时间】:2020-05-29 15:54:00
【问题描述】:
我想使用 Fetch POST 请求将数据保存到我服务器上的静态 JSON 文件。
这可能吗?如果是这样,任何提示或建议将不胜感激
我的 Javascript 代码尝试将数据保存到我的 JSON 文件中。
fetch('link-data.json', {
method:'POST',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-type':'application/json'
},
body:JSON.stringify({
id:3,
title:"website_title",
url:"www.example.com",
description:"Hi_there_im_a_description"
})
})
.then((res) => res.json())
.then((data) => console.log(data))
我想要将数据保存到的 JSON 文件。
[
{
"id": 0,
"title": "Twitter. It's what's happening.",
"url": "https://twitter.com/?lang=en",
"description": "From breaking news and entertainment to sports and politics, get the full story with all the live commentary."
},
{
"id": 1,
"title": "Netflix - Watch TV Shows Online, Watch Movies Online",
"url": "https://www.netflix.com/",
"description": "Watch Netflix movies & TV shows online or stream right to your smart TV, game console, PC, Mac, mobile, tablet and more."
},
{
"id": 2,
"title": "Facebook - Log In or Sign Up",
"url": "https://www.facebook.com/",
"description": "Create an account or log into Facebook. Connect with friends, family and other people you know. Share photos and videos, send messages and get updates."
}
]
【问题讨论】:
-
服务器代码在哪里?
-
@CaduDeCastroAlves 目前我没有:O
-
您不能通过 JavaScript 在服务器上写入文件。您应该使用像 PHP 或 NodeJS 这样的服务器端语言来执行此操作。您已经知道哪种服务器端语言?
-
有可能,但您正尝试将 JSON 发送到
www.example.com。您需要设置自己的端点才能使其正常工作。 -
哦,你是对的。我假设您的请求正文是您要发送的 JSON。我的观点仍然存在。您需要创建一个端点来命中。
标签: javascript json post fetch persistent-storage