Jump to content

Ajax -- Putting Text To Txt File On Server


sumchurch

Recommended Posts

I got how to get a text file loaded from the server with AJAX -- works well. Now I would like the user to be able to edit the file and put it back on the server. I noticed that there is a "GET" switch and talk about a "PUT" -- but no examples on W3Schools. Can this be done, and how?

Link to comment
Share on other sites

It can be done with GET or POST, the same way a form operates. The differences are small.Technically, POST is correct because that is what you are doing. Traditionally, there are also length limits to GET data, but no real limits to the length of POST data.For the benefit of your server script, form your POST data string the same way you form a GET data string -- that is, as key-value pairs. Pass each key and value through encodeURIComponent() before assembling them with the & delimiter, or your server may goof up the pair relationships, truncate items, and do other weird things. If you're using PHP, your server should decode the data without your doing anything extra to it. If you're using something like Perl, you already know how to decode it.POST data gets passed as the argument to your AJAX object's .send method. If you're accustomed to the GET technique, your send() method probably gets passed a null value. For POST, you pass the data as AJAX.send(data) -- that's the only way your server script will be able to find it. Like, if you're using PHP, that's the only way to push data into your $_POST array.Obviously, the first argument of your .open method should be "POST" instead of "GET".The rest is up to your server itself.There is currently no mechanism to upload a file with AJAX. But it sounds like you have a big text string that you're accessing with JavaScript, so POST is the way to go.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...