2014-10-28 48 views
0

我试图使用Ajax调用提交表单数据。相关的表格代码就像。在Ajax调用上获取表单数据在节点服务器端

<form target="_blank" id="addCaseForm" class="form-horizontal col-md-12" action="" method="POST"> 
    <div class="form-group"> 
     <label class="col-md-3 control-label">Patient Name</label> 
     <div class="col-md-9"> 
      <input type="text" class="form-control" id="patientName" name="patientName" placeholder="" required> 
     </div> 
    </div> 
    .. .. .. .. .. 
    <button type="submit" class="btn btn-primary btn-xs">Save</button> 

</form> 

在提交按钮下面的事件获取调用那里我做正常的Ajax调用的Node.js服务器提交表单数据的点击。首先,我已经序列化表单数据并将其作为附加参数传递给request.sent。

但是如何在服务器端获取这些数据。我尝试了多种方式获取像req.body这样的数据。 patientNamereq.params('patientName')但没有任何工作。我错过了一些东西。

$("#addCaseForm").submit(function(event) { 
    var postData = $(this).serialize(); 
    // "patientName=rtgh&patientFatherName=5tryh&patientDob=10%2F08%2F2014&patientBloodGroup=o&patientAge=25&patientSex=M&patientNationality=Indian&patientPhone=76&Specilizationtag=3&patientDesc=uyhjn" 
    event.preventDefault(); 
    var request;  
    try { 
     request = new XMLHttpRequest(); 
    } catch (tryMS) { 
     try { 
      request = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (otherMS) { 
      try { 
       request = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (failed) { 
       request = null; 
      } 
     } 

    } 

    if (request == null) { 
     console.log("Error::Unable to create request object at add new case"); 
     return false; 
    } 
    request.open('POST', '/addNewCase', true); 
    request.onreadystatechange = function() { 
     if (request.readyState == 4) { 
      if (request.status == 200) { 
       console.log("Form successfully submitted"); 
      } 
     } 
    }; 
    request.send(postData); 
}); 

如何在节点服务器端获取表单数据?

我使用快递和节点代码低于

exports.addNewCase = function(req,res){ 
    var Specilizationtag = [], 
     attachemntTag = []; 
    Specilizationtag = req.params('Specilizationtag').split(',').map(function(eachTag){ 
     return parseInt(eachTag); 
    }); 


    var patient = new Patient({ 

      name: req.params('patientName'), 
      fatherName: req.params('patientFatherName'), 
      dob:new Date(req.params('patientDob')), 
      bloadGroup : req.params('patientBloodGroup'), 
      age:req.params('patientAge'), 
      sex: req.params('patientSex'), 
      nationality:req.params('patientNationality'), 
      phone: req.params('patientPhone'), 
      description: req.params('patientDesc'), 
      specialization:Specilizationtag, 
      attachmentId:attachemntTag}); 

    patient.save(function(err){ 
     if(err) {console.log("Error Occured"+err);}; 

    }); 
    res.end(); 
}; 
+0

您正在发送带有POST请求的数据,并尝试使用GET参数在服务器端检索它。 – 2014-10-28 10:55:25

+0

如何使用POST获取数据?@BenFortune – Sumeet 2014-10-28 10:57:08

+0

使用[body-parser](https://github.com/expressjs/body-parser)中间件。 – 2014-10-28 11:02:13

回答

0

添加在固定的问题请求对象的内容类型。

调用setRequestHeader()并将其内容类型设置为“application/x-www-form-urlencoded”。这对于通过Ajax进行的任何POST请求都是必需的。

request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
相关问题