簡體   English   中英

在Ajax調用的節點服務器端獲取表單數據

[英]Get form data at node server side on Ajax call

我正在嘗試使用Ajax call提交表單數據。 相關的表單代碼如下所示。

<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>

在單擊“提交”按鈕后,將調用以下事件,在這里我對node.js服務器進行正常的Ajax調用以提交表單數據。 首先,我已序列化表單數據,並將其作為附加參數傳遞給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();
};

在請求對象中添加內容類型可解決此問題。

調用setRequestHeader()並將其內容類型設置為“ application / x-www-form-urlencoded”。 通過Ajax發出的任何POST請求都需要此文件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM