简体   繁体   English

如何使用JavaScript永久修改单独文件上的JSON对象?

[英]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)来执行此操作。

Some Points: 一些要点:

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.

相关问题 如何在 NextJS 中将 json 文件(或 JavaScript 对象)上传到 IPFS? - How can I upload a json file (or JavaScript Object) to IPFS in NextJS? 如何在javascript中更改json文件中的对象? - How can I change object in json file in javascript? 如何通过从单独的数组中匹配的字符串对象值从 3 个嵌套的 json 数组中提取对象值,然后在 html/javascript 中显示它 - How can I extract object values from 3 nested json arrays by matched string object values from a separate array and then display it in html/javascript 如何在javascript中过滤和修改数组中的每个对象? - How can i filter and modify each object in array in javascript? 如何在 javascript 中修改单个 object 中的索引 - How can i modify indexing in a single object in javascript 如何使用JavaScript永久更改网站中的CSS文件? - How can I permanently change css file in my website using javascript? 如何修改每个 JSON 对象 javascript - How to modify each JSON object javascript 如何在javascript中解析此JSON对象? - How can I parse this JSON Object in javascript? 如何在 JavaScript 中过滤 JSON 对象? - How can I filter a JSON object in JavaScript? JAVASCRIPT:如何删除json对象中的json对象? - JAVASCRIPT: how can I remove a json object in json object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM