I'd like to send JSON form data to websocket server instead of submitting the form and reload the page.
In AJAX it is pretty simple and straightforward thanks to native FormData() web-api object:
myform.addEventListener("submit",function(e) {
e.preventDefault();
var xhr = new XMLHttpRequest();
xhr.onload = myFormHandler.bind(xhr);
xhr.open('POST',myform.action,true);
xhr.send(new FormData(myform));
}
It would be nice to have similar functionality in webSockets, to transform the form data into JSON object and use it like this:
myWebSocket.omessage = myFormHandler;
myform.addEventListener("submit",function(e) {
e.preventDefault();
myWebSocket.send(JSONformData);
}
But the reference (see the link above) says that FormData has only append method, I don't know how to send the form data to the websocket server and extract them there.
On code.google.com I've found something very close to what I need: form2js but it can't handle files (it can be done using FileReader.readDataAsURL). Should I add the file handling functionality into that code, or is there any native solution like FormData object?