javascript - Get form data at node server side on Ajax call -


I am trying to submit form data using Ajax calls. The relevant form becomes like a code

 . & Lt; Form target = "_ blank" id = "addCaseForm" class = "form horizontal call-MD-12" action = "" method = "post" & gt; & Lt; Div class = "form-group" & gt; & Lt; Label class = "col-md-3-control-label" & gt; Patient's name & lt; / Label & gt; & Lt; Div class = "col-md-9" & gt; & Lt; Input type = "text" class = "form-control" id = "patientName" name = "patientName" placeholder = "" required & gt; & Lt; / Div & gt; & Lt; / Div & gt; .. .. .. .. .. .. & lt; Button type = "submit" class = "btn btn-primary btn-x" & gt; Save & lt; / Button & gt; & Lt; / Form & gt;  

Click the Submit button The following event is being called where I have to submit the form data to the node. Calling the normal Ajax on the JS server First of all, I have serialized the form data and sent it as request parameter to an additional parameter.

But how to get this data into server data I tried several ways to get data such as req.body ; req.params ('Patient name' ) But nothing worked. I'm missing something

  $ ("# addCaseForm") presented (function (event) {var POSTDATA = $ (this) .serialize (); .. // "patientName = rtgh & Amp; patientFatherName = 5tryh & patientDob ​​= 10% 2F08% 2F2014 & patientBloodGroup = o & amp; patientAge = 25 & patientSex = M & patientNationality = Indian & amp; patientPhone = 76 & amp; ; Specilizationtag = 3 & patientDesc = uyhjn "event.preventDefault (); on request; try {request = new XMLHttpRequest ();} hold (tryMS) {try {request = new ActiveXObject (" Msxml2.XMLHTTP ");} Hold (otherMS) {try = request = new ActiveXObject ("Microsoft.XMLHTTP");} hold (failed) {request = null;}}} if (request == faucet) {console.log ("Error :: new topic Add request request Unable to make Jake "); Return return;} request.open ('POST' '/ AddNewCase' is true); request.onreadystatechange = function () {iF (request.readyState == 4) {if (request. Status == 200) {console.log ("submit form successfully")}}}} request.send (POSTDATA);});  

How to obtain form data on the node server side?

I am using express and node code

  export.addNewCase = function (rik, ridge) {var specifiaggate = [], atachment = []; SPLICISItag = reCAPRAMP ('clarification'). the division (','). Map (function (each tag) {return perieth (each tag);}); Patient = new patient ({name: req.params ('patientName'), fatherName: req.params ('patientFatherName'), Date of birth: new date (req.params ('patientDob')), bloadGroup: req.params Req.params ('patientSex'), nationality: req.params ('patientNationality'), phone: req.params ('patientPhone'), Description: Rake Paramas ('Patient DCC'), Expertise: SpelliciTag, Attachment ID: Attachment Tag}}; Patient.save (function (error) {if (err) {console.log ("error occurred" + err);};}); Res.end (); };  

Adding content types to the requested object fixes the problem.

Set call setRequestHeader () and its content type to "application / x-www-form-urlencoded". This is necessary for any post request made through Ajax.

  request.setRequestHeader ("content type", "app / x-www-form-url expired");  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -