Using the FormData interface
One of the new features added to XMLHttpRequest
Level 2 (http://www.w3.org/TR/XMLHttpRequest2/) is the FormData
object. This enables us to use a set of key-value pairs that can be sent using AJAX. The most common use is in sending binary files or any other large amount of data. In this recipe, we will create two scripts that will send FormData
, one with a plain JavaScript and the other with jQuery, as well as the server-side code to support it.
Getting ready
The server will be done in Nodejs using restify (http://mcavage.github.io/node-restify/). In order to install the dependencies, a package.json
file can be created where restify will be added.
How to do it...
The server should be able to accept
HTTP POST
with typemultipart/form-data
; that is why there is a built-in plugin forrestify
calledBodyParser
. This will block the parsing of the HTTP request body:var server = restify.createServer(); server.use(restify.bodyParser({ mapParams: false })); server.post('hi...