我把这个变量放入后,得到一个错误函数进行ajax调用

时间:2017-07-01 08:40:10

标签: javascript jquery html ajax

出于某种原因,每当我放置"ntaReqRef": ntaReqRef时,我一直在ajax调用中获得错误功能但是当我不包含它时,我的ajax调用工作正常,所以我不知道它是非法的还是它是否与我的代码有关,因为当我执行代码时,它在node.js中显示正确的响应,但随后给出了一个错误函数。

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

感谢任何帮助。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我认为问题出在您的html输入字段

尝试更改标有id&#39; ntaReqRefa&#39;的输入字段。如下所示

<input type="text" id="ntaReqRefa" name= "ntaReqRef" required  
autocomplete="off"/>