[英]I can retrieve the data from the JSON file, but I can't add it
I read that question( click ). 我读了那个问题( 点击 )。 I read in similar articles.
我读了类似的文章。 But it's not sending the data.
但它并没有发送数据。
Example JSON file: 示例JSON文件:
{
"jack":"jack123",
"rick":"rick123",
"maria":"maria123"
}
HTML CODE: HTML代码:
<input type="text" id="username" placeholder="username">
<input type="text" id="password" placeholder="password">
<button onclick=run()>CLICK RUN</button>
<button onclick=run1()>CLICK RUN1</button>
JAVASCRIPT CODE: JAVASCRIPT代码:
function run1(){
var username = $("#username").val();
var password = $("#password").val();
let xmlRequest,obj;
xmlRequest = new XMLHttpRequest();
xmlRequest.onreadystatechange = function(){
if(this.readyState === 4 && this.status === 200){
obj = JSON.parse(this.responseText);
var res = obj[username];
if(res == password){
alert(`Welcome ${username}`);
}
}
}
xmlRequest.open("GET","http://localhost/deneme/info.json",true);
xmlRequest.send();
}
function run(){
var username = $("#username").val();
var password = $("#password").val();
let xmlRequest,obj;
xmlRequest = new XMLHttpRequest();
var url = "http://localhost/deneme/info.json";
xmlRequest.open("POST", url, true);
xmlRequest.onreadystatechange = function(){
if(this.readyState === 4 && this.status === 200){
obj = JSON.parse(this.responseText);
}
}
var data = JSON.stringify({username:password});
xmlRequest.send(data);
}
NOTE: I don't know PHP. 注意:我不懂PHP。
There are plenty of problems in what you are trying to do. 你要做的事情有很多问题。 Even if you are front-end, you need to know how back-end works.
即使你是前端,你也需要知道后端是如何工作的。 Go with node.js, it uses javascript to build simple servers.
使用node.js,它使用javascript来构建简单的服务器。
What you are doing is sending request, but it cannot be processed because you don't have access to file system of your computer from client-side javascript. 你正在做的是发送请求,但它无法处理,因为你无法从客户端javascript访问你的计算机的文件系统。
How it works: You send post request with some data. 工作原理:您发送带有一些数据的发布请求。 Your back-end gets this data and does smth with it.
你的后端获取这些数据并与之相关。 Writes to file or to database or performs any other action.
写入文件或数据库或执行任何其他操作。 What you are missing is server-side.
你缺少的是服务器端。 Again, watch some tutorial about how to install node.js, npm, how to make simple server.
再次,观看一些关于如何安装node.js,npm,如何制作简单服务器的教程。
Besides that, you mustn't store logins and passwords in text files, and especially without encryption. 除此之外,您不得将登录名和密码存储在文本文件中,尤其是不加密。 This is a no-no.
这是禁忌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.