0
出於某種原因,我不斷收到錯誤功能Ajax調用每當我把這個"ntaReqRef": ntaReqRef
但是當我不包括它在我的Ajax調用完美的作品,所以我卡住至於它是否是非法的,還是它與我的代碼有關,因爲當我執行代碼時,它會在node.js中顯示正確的響應,但會提供一個錯誤函數。獲得誤差函數做Ajax調用一次,我把這個變量
function create(){
var samId = $('#samIda').val();
var itemDescrip = $('#itemDescrip').val();
var issueQty =$('#issuedQty').val();
var openQty = $('#openingQty').val();
var closeQty = $('#closingQty').val();
var corrupQty = $('#corruptedQty').val();
var remarks = $('#Remarks').val();
var ntaReqRef = $('#ntaReqRefa').val();
var postData = {
"samId": samId , "itemDescrip": itemDescrip, "issueQty" :
issueQty,"openQty" : openQty, "closeQty" :closeQty,
"corrupQty": corrupQty, "remarks": remarks, "ntaReqRef":
ntaReqRef
};
var postJSON = JSON.stringify(postData);
$.ajax({
url: "http://localhost:3000/api/insertRecord", // server url
type: "POST", //POST or GET
contentType: "application/json", // data to send in ajax format or querystring format
data: postJSON,
dataType : "json", //dataType is you telling jQuery what kind of
response to expect
success: function(response) {
alert('success');
$("#Iresult").html("A record has been created. :D");
},
error: function(response) {
alert('error' + response);
}
});
}
我的關於變量有問題的html代碼。
<div class="field-wrap">
<label>
NTA SAM Ref Number<span class="req">*</span>
</label>
<input type="NtaReqRef" id="ntaReqRefa" name= "ntaReqRef" required
autocomplete="off"/>
</div>
<input class="button" type="submit" id="insert" value="Create"
onclick="create()" />
</form>
<div id= "Iresult"></div>
這是我的服務器端代碼。
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';
var insertDocument = function(db, req, callback) {
db.collection('documents').insertOne({
//'_id': Object.keys(obj).length,
'samID': req.body.samId,
'itemDescription': req.body.itemDescrip,
'issuedQTY': req.body.issueQty,
'openingQTY':req.body.openQty,
'closingQTY':req.body.closeQty,
'corruptedQTY':req.body.corrupQty,
'Remarks':req.body.remarks,
'ntaSamRequestRef': req.body.ntaReqRef
//'Created Date': "<b>" + day + "/" + month + "/" + year + "</b>"
}, function(err, results) {
//assert.equal(err, null);
if(err) return callback(err);
console.log("Inserted a document into the documents collection.");
console.log(results);
var cursor = db.collection('documents').find({
//'_id': Object.keys(obj).length,
'samID': req.body.samId
}).toArray(function(err, doc) {
if (err) {
return callback(err);
} else{
console.log('Successfully queried');
console.log(doc);
return callback(null, JSON.stringify(doc));
}
});
});
};
module.exports = {
postCollection : function(req,res){
var samId = req.body.samId;
var itemDescrip = req.body.itemDescrip;
var issueQty = req.body.issueQty;
//var indexNo = Object.keys(obj).length;
var openQty = req.body.openQty;
var closeQty = req.body.closeQty;
var corrupQty = req.body.corrupQty;
var remarks = req.body.remarks;
var ntaReqRef = req.body.ntaReqRef;
//var createdDate = "<b>" + day + "/" + month + "/" + year + "</b>"
MongoClient.connect(url, function(err, db) {
//assert.equal(null, err);
if(err) {
res.send(err);
res.end();
}
insertDocument(db, req, function(err,doc) {
if(err)
res.send(err);
else{
setTimeout(function(){
res.send(doc);
res.end();
},2000);
}
db.close();
});
});
}
}
任何幫助表示讚賞。提前致謝!
您使用的是這個什麼bodyParser樣?奇怪的是,發送字符串化的JSON,而不是www-url-encoded數據,將會用任何東西填充「req.body」? – adeneo
@adeneo bodyParser正常的一個 –