Is it possible to get an XML file (not HTML) from a server, add/remove/edit particular parts of it with client-side JavaScript, and then send it back to the server to save it? JSON or any other markup/data interchange format works too.
-
Are you going to be saving it back to the server with a server side language (PHP, .NET, etc...)? Or are you also asking if you can save it to the server with javascript?Nathan Koop– Nathan Koop2009-06-19 14:42:46 +00:00Commented Jun 19, 2009 at 14:42
-
Is it even possible to save it to the server with JavaScript? In any case, I plan to use CGI.Skofo– Skofo2009-06-19 14:57:41 +00:00Commented Jun 19, 2009 at 14:57
-
Javascript is a client-side language. But you can use XmlHttpRequest to make a programmatic POST to a CGI script that contains logic to save it locally.Josh Stodola– Josh Stodola2009-06-19 18:31:50 +00:00Commented Jun 19, 2009 at 18:31
5 Answers
Certainly. You can use the XMLHttpRequest object to make the request for the file, do any operations you need to the data, and then post the entire document back using another XMLHttpRequest. You could do this with XML (and that is probably easiest for downloading the original document), but you would probably have the easiest time using JSON for the post back to the server.
You will need a server-side script (i.e. PHP, ASP, Ruby) to receive the posted data, format it however desired (i.e. turn the JSON into an XML document) and save it either as a file or in a database.
This question is far too general to get into specific implementation yet, but if you need additional help with these steps just ask.
2 Comments
Sure. You can use an XMLHttpRequest to fetch an XML document if the server serves it using the text/xml MIME type. The responseText property will give you the XML text, but the browser will also parse the XML for you and provide a DOM tree in responseXML. You can modify that DOM as you please and then serialize it and send it back to the server.
You can also use JSON the same way. You use XMLHttpRequest to get the data from the server, then jsonData = eval(xhr.responseText) to get turn the JSON data into JavaScript objects.
Every major JavaScript library has modules/functions to aid doing either of these methods. XML and JSON are the two most popular data exchange methods in Ajax applications.