[英]How can I permanently modify a JSON object on a separate file with JavaScript?
I've been trying for a while to modify my json formatted database (db.txt) which lies on a server with JS, but can't get the file to change. 我已经尝试了一段时间,以修改位于JS服务器上的json格式数据库(db.txt),但无法更改文件。 It loads into my JS script perfectly with ajax GET, but I can't make it change. 它可以通过ajax GET完美地加载到我的JS脚本中,但是我无法对其进行更改。
Here is the code I'm trying and failing with: 这是我尝试并失败的代码:
$.ajax
({
type: "POST",
url: 'db.txt',
dataType: 'json',
async: false,
data: {"test": "test23123"},
success: function () {
alert("posting success!!!!");
}
})
Here is the db.txt: 这是db.txt:
{
"Hoegaarden, Wit-Blanche": {"namn" : "Hoegaarden, Wit-Blanche", "pris" : 20, "volym" :
500, "uppdaterad" : "2013-06-27"},
"Jaegermeister": {"namn" : "Jaegermeister", "pris" : 150, "volym" : 700, "uppdaterad" : "2013-06-27"},
"Miller": {"namn" : "Miller", "pris" : 20, "volym" : 500, "uppdaterad" : "2013-06-27"}
}
It gives me the success alert when I run it, but nothing happens to the txt.db. 当我运行它时,它会给我成功警报,但是txt.db没有任何反应。 Can I modify it to work? 我可以对其进行修改吗? If not, what method could I use instead? 如果没有,我可以使用哪种方法代替?
Thanks 谢谢
JavaScript cannot modify files on the server. JavaScript无法修改服务器上的文件。 You'll need a server-side language (like PHP) to do it. 您将需要服务器端语言(例如PHP)来执行此操作。
.json
extension. 包含JSON的文件应以.json
扩展名结尾。 dataType
is the type of data you're expecting. dataType
是您期望的数据类型。 contentType
is the type of data you're transferring (that's what you want). contentType
是您正在传输的数据类型(这就是您想要的)。 You cannot modify the file that resides on the server that way. 您不能以这种方式修改驻留在服务器上的文件。 JavaScript has no access to the file on the server. JavaScript无法访问服务器上的文件。 Instead, you need to expose a URL through a server-side app that does this for you. 相反,您需要通过为您执行此操作的服务器端应用程序公开URL。 You can do this with something like PHP, Java, or Node.js. 您可以使用PHP,Java或Node.js之类的方法来执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.