Reputation: 703
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. It loads into my JS script perfectly with ajax GET, but I can't make it change.
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:
{
"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. Can I modify it to work? If not, what method could I use instead?
Thanks
Upvotes: 0
Views: 779
Reputation: 175017
JavaScript cannot modify files on the server. You'll need a server-side language (like PHP) to do it.
.json
extension.dataType
is the type of data you're expecting. contentType
is the type of data you're transferring (that's what you want).Upvotes: 3
Reputation: 95578
You cannot modify the file that resides on the server that way. JavaScript has no access to the file on the server. Instead, you need to expose a URL through a server-side app that does this for you. You can do this with something like PHP, Java, or Node.js.
Upvotes: 2