Ajax:将表单数据附加到json文件而不需要php
我已经尝试了好几个小时才让这东西发挥作用。 我正在尝试将标题文本和内容文本附加到 .json 文件中。我在这里看到了很多类似的问题,但是对于 php,我不被允许使用它(老师的规则)。
我尝试了 Fetch API,但发现它只处理来自 json 文件的 get 请求。然而我只在这里找到了 ajax 函数,它们在前面使用了 $ 。
我只是不知道如何在不使用 php 的情况下包含 json 文件的位置。名称文件 = posts.json。 json 内的示例: 图片json
这是我的代码JS:
let form = document.getElementById('frm');
form.addEventListener('submit', PostBlog)
function PostBlog(){
let title = document.getElementById("txtTitle");
let content = document.getElementById("txtContent");
let data = {title1: title.value}
//let url = new URL("http://localhost:8080/admin.html");
//let parms = new URLSearchParams({title1: title.value, content1: content.value});
fetch("http://localhost:8080/admin.html",
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
}
).then(receive).then(response => response.text()).then(Succeed).catch(problem)
}
function receive(response) {
if(!(response.ok)){
throw new Error("Didn't Receive " + response.status);
}
return response;
}
function problem(error){
alert("Not Found " + error);
}
function Succeed(data){
alert("Found " + data);
}
HTML重要部分:
<form id="frm">
<div class="form-group">
<label for="txtTitle">Title</label>
<input type="text" class="form-control" id="txtTitle">
</div>
<div class="form-group">
<label for="txtContent">Content</label>
<textarea class="form-control" id="txtContent" rows="3"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-primary">Reset</button>
</div>
</form>
最后是网站的图片
< a href="https://i.sstatic.net/WAA3Q.png" rel="nofollow noreferrer">网站图片
I've been trying for hours to get this thing working.
I'm trying to append the text of Title and the text of Content to a .json file. I've seen a lot of similar problems on here but with php and I'm not allowed to use it (Rule of the teacher).
I tried Fetch API but I found out that it only handles get requests from json files. Yet I only found ajax functions on here where they use $ in front of them.
I just don't know how to include the location of the json file without using php. name file = posts.json.
Example inside json:
Picture json
This is my code JS:
let form = document.getElementById('frm');
form.addEventListener('submit', PostBlog)
function PostBlog(){
let title = document.getElementById("txtTitle");
let content = document.getElementById("txtContent");
let data = {title1: title.value}
//let url = new URL("http://localhost:8080/admin.html");
//let parms = new URLSearchParams({title1: title.value, content1: content.value});
fetch("http://localhost:8080/admin.html",
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
}
).then(receive).then(response => response.text()).then(Succeed).catch(problem)
}
function receive(response) {
if(!(response.ok)){
throw new Error("Didn't Receive " + response.status);
}
return response;
}
function problem(error){
alert("Not Found " + error);
}
function Succeed(data){
alert("Found " + data);
}
HTML important part:
<form id="frm">
<div class="form-group">
<label for="txtTitle">Title</label>
<input type="text" class="form-control" id="txtTitle">
</div>
<div class="form-group">
<label for="txtContent">Content</label>
<textarea class="form-control" id="txtContent" rows="3"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-primary">Reset</button>
</div>
</form>
And finally a picture of the site
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ajax 是一个术语,意思是“从 JavaScript 发出 HTTP 请求,在浏览器中运行,无需离开页面”。
您不能使用它直接写入服务器上的数据,因为浏览器不允许简单地写入服务器。如果可能的话,那么 Google 主页每秒就会被破坏 30 次。
您需要一个服务器端进程来接受 HTTP 请求并将数据写入文件(尽管数据库通常是更好的选择,因为它可以解决同时写入等问题)。如果您不想使用 PHP,那么您可以使用服务器支持的任何其他服务器端编程语言。如果您不想使用其中任何一种,则可以将服务器更改为支持您要使用的语言的服务器。
Fetch API 可用于发出或多或少任何类型的请求、任何类型的负载和任何类型的响应。
浏览器有两个用于发出 Ajax 请求的 API。 XMLHttpRequest 和 Fetch。 (获取较新)。
有许多第三方库包装 XMLHttpRequest 和/或 Fetch 以提供替代 API。
$
是 jQuery 通常分配的变量。它的主要 Ajax 辅助函数可以在ajax
属性中找到。使用第三方库并不能解决浏览器无法写入服务器的问题。
Ajax is a term meaning "Making an HTTP request from JavaScript, running in the browser, without leaving the page".
You can't use it to directly write to data on the server, because browsers aren't allowed to simply write to servers. If that was possible, then the Google homepage would be vandalised 30 times per second.
You need a server-side process to take the HTTP request and write the data to the file (although a database is usually a better bet as that will take care of problems like simultaneous writes). If you don't want to use PHP then you can use any other server-side programming language the server supports. If you don't want to use any of those then you can change your server to one that supports the language you want to use.
The Fetch API can be used to make more-or-less any kind of request with any kind of payload and any kind of response.
Browsers have two APIs for making Ajax requests. XMLHttpRequest and Fetch. (Fetch is newer).
There is numerous third-party libraries that wrap XMLHttpRequest and/or Fetch to provide alternative APIs.
$
is the variable to which jQuery is commonly assigned. Its main Ajax helper function can be found in theajax
property.The use of a third-party library won't solve the issue that browsers can't write to servers.