2017-07-03 29 views
0

没有这个变量ntaReqRef,我的ajax很好地工作,但是一旦我添加了这个变量,它只是继续去ajax的错误函数。这是我的服务器端代码:每次获取ajax错误我包含变量ntaReqRef

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,它像这样[{"_id":"5959e2448dd75c0908cad3ac","samID":"10","itemDescription":"10","issuedQTY":"1","openingQTY":"1","closingQTY":"1","corruptedQTY":"1","Remarks":"1","ntaSamRequestRef":"11-12"}]

至于我的HTML代码的响应工作,

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

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); 
     } 
    }); 


}  

这是错误,当我在误差函数把console.log(response)

对象{readyState的:0,getResponseHeader:功能,getAllResponseHeaders:功能,setRequestHeader:功能,overrideMimeType:函数...}

所以我不完全知道什么是错的。我的cors工作,我怀疑我的dataType是错误的,因为我做JSON.stringify(doc)然后发送到响应res.send(doc)。至于解析错误,我不认为我的回应有解析错误,因为我已经字符串化。我使用node.js作为连接到mongodb的服务器端代码。任何帮助表示赞赏!希望有人能够通过我的ajax调用来检测问题。提前致谢!

回答

0

事实证明,我需要在我的ajax调用event.preventDefault()使它成为一个成功的事件,这是相当奇怪的,所以如果任何人有一个解释,随意回答我会接受答案。