To make an ajax request in node.js not obviously how it works, there not exists $_FILE array like in PHP and you have to install a few modules for extracting the passed data.
Types of forms
There could be two type forms, a form that passed files to a server (multipart/form-data) and a form that passes only data.
For the forms with multipart data, we use one of the module formidable or others.
For the forms without multipart we use simply body-parser
Code for sending the ajax request to the server
The code is different for the two types of forms
Code for the forms without sending files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
jQuery.ajax({ url: '/login', type: 'POST', data: { name: name, pass: pass, }, // processData: false, //allow send not only string // contentType: false, // async: false,// waiting till finished handling request success: function(msg){ console.log("msg=",msg); } });//end ajax |
and the code for the forms with multipart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
jQuery.ajax({ url: '/login', type: 'POST', data: { name: name, pass: pass, }, processData: false, //allow send not only string contentType: false, // async: false,// waiting till finished handling request success: function(msg){ console.log("msg=",msg); } });//end ajax |
the difference with multipart have to be this two rows
1 2 |
processData: false, //allow send not only string contentType: false, |
without it will not work