この変数ntaReqRefがなければ、私のajaxはうまく動作しますが、この変数を追加すると、ajaxのエラー関数に移ります。これは私のサーバー側のコードです:変数ntaReqRefを含めるたびにajaxエラーを取得
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) {
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,
'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>"
}).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();
});
});
}
}
私は郵便配達の私のAPIをテストして、それは私のhtmlコードについては、この[{"_id":"5959e2448dd75c0908cad3ac","samID":"10","itemDescription":"10","issuedQTY":"1","openingQTY":"1","closingQTY":"1","corruptedQTY":"1","Remarks":"1","ntaSamRequestRef":"11-12"}]
のようなレスポンスと連携し、
<div id="insert">
<h1>Add a New Record</h1>
<form class="wrapper-input" method="POST" onsubmit="create()" >
<div class="field-wrap">
<label>
SAM ID<span class="req">*</span>
</label>
<input type="samId" id="samIda" name= "samId" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Item Description<span class="req">*</span>
</label>
<input type="itemDescrip" id="itemDescrip" name= "itemDescrip" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Issued QTY<span class="req">*</span>
</label>
<input type="issuedQty" id="issuedQty" name= "issuedQty" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Opening QTY<span class="req">*</span>
</label>
<input type="openingQty" id="openingQty" name= "openingQty" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Closing QTY<span class="req">*</span>
</label>
<input type="closingQty" id="closingQty" name= "closingQty" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Corrupted QTY<span class="req">*</span>
</label>
<input type="corruptedQty" id="corruptedQty" name= "corruptedQty" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Remarks<span class="req">*</span>
</label>
<input type="Remarks" id="Remarks" name= "Remarks" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
NTA SAM Ref Number<span class="req">*</span>
</label>
<input type="text" id="ntaReqRef" name= "NtaReqRef" oninput="hyphen()" required autocomplete="off"/>
</div>
<input class="button" type="submit" id="inserta" value="Create" />
</form>
<div id= "Iresult"></div>
</div>
これは私ですjsコードはhtmlに接続されています。私は誤差関数で
オブジェクト{0、getResponseHeader:関数、getAllResponseHeaders:関数、setRequestHeader:関数、overrideMimeType:readyStateの機能を...} console.log(response)
を置くとき
function hyphen(){
$('#ntaReqRef').keyup(function() {
var foo = $(this).val().split("-").join(""); // remove hyphens
if (foo.length > 0) {
foo = foo.match(new RegExp('.{1,2}', 'g')).join("-");
}
$(this).val(foo);
});
}
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 = $('#ntaReqRef').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');
console.log(response);
}
});
}
これがエラーであった
私は何が間違っているのか正確には分かりません。私のcorsはうまくいき、私はJSON.stringify(doc)
なので、私のデータタイプは間違っているとは思っていません。それは応答res.send(doc)
に送られます。解析エラーに関しては、私が既に文字列化しているので、私の応答に解析エラーがあるとは思わない。私はmongodbに接続されているサーバーサイドのコードにnode.jsを使用しています。どんな助けもありがとう!うまくいけば、私のajax呼び出しで問題を検出できる人がいます。前もって感謝します!