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這樣的數據。 patientName,req.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();
};
您正在發送帶有POST請求的數據,並嘗試使用GET參數在服務器端檢索它。 – 2014-10-28 10:55:25
如何使用POST獲取數據?@BenFortune – Sumeet 2014-10-28 10:57:08
使用[body-parser](https://github.com/expressjs/body-parser)中間件。 – 2014-10-28 11:02:13