Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将参数传递到node.js中的路由_Javascript_Jquery_Ajax_Node.js - Fatal编程技术网

Javascript 将参数传递到node.js中的路由

Javascript 将参数传递到node.js中的路由,javascript,jquery,ajax,node.js,Javascript,Jquery,Ajax,Node.js,我对网络开发非常陌生。我曾经用WPF和C做桌面开发。现在我正在学习Node.js 我有一个名为Party.js的模型,其中我定义了两个导出,如下所示: module.exports.getAllParties = function(callback){ Party.find().lean().exec(function(err, parties){ if (err) return callback(err, null); callback(null, parties);

我对网络开发非常陌生。我曾经用WPF和C做桌面开发。现在我正在学习Node.js

我有一个名为Party.js的模型,其中我定义了两个导出,如下所示:

module.exports.getAllParties = function(callback){
  Party.find().lean().exec(function(err, parties){
    if (err) return callback(err, null);
    callback(null, parties);
  });
};

module.exports.getPartyByPartyCode = function(partyCode, callback){
  Party.find({partyCode: partyCode}).exec(function(err, party){
    if(err) return callback(err, null);
    callback(null, party);
  });
};
router.get('/', function(req, res, next){

  //retrieve all parties from Party model
  Party.getAllParties(function(err, parties) {
        if (err) {
            return console.error(err);
        } else {
            //respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
            res.format({

              //response in dust or jade files
              html: function(){
                  res.render('Party', {
                        title: 'Party',
                        "parties" : parties
                    });
              },

              //JSON response will show all parties in JSON format
              json: function(){
                  res.json(parties);
              }
          });
        }
  });
});

router.get('/:partyCode', function(req, res, next){

  Party.getPartyByPartyCode(function(err, party) {
        if (err) {
            return console.error(err);
        } else {
            //respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
            res.format({

              //response in dust or jade files
              html: function(){
                  res.render('Party', {
                        title: 'Party',
                        "party" : party
                    });
              },

              //JSON response will show all parties in JSON format
              json: function(){
                  res.json(party);
              }
          });
        }
  });
});
现在,我还有一个名为Party.js的路由,其中我有两个get方法,如下所示:

module.exports.getAllParties = function(callback){
  Party.find().lean().exec(function(err, parties){
    if (err) return callback(err, null);
    callback(null, parties);
  });
};

module.exports.getPartyByPartyCode = function(partyCode, callback){
  Party.find({partyCode: partyCode}).exec(function(err, party){
    if(err) return callback(err, null);
    callback(null, party);
  });
};
router.get('/', function(req, res, next){

  //retrieve all parties from Party model
  Party.getAllParties(function(err, parties) {
        if (err) {
            return console.error(err);
        } else {
            //respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
            res.format({

              //response in dust or jade files
              html: function(){
                  res.render('Party', {
                        title: 'Party',
                        "parties" : parties
                    });
              },

              //JSON response will show all parties in JSON format
              json: function(){
                  res.json(parties);
              }
          });
        }
  });
});

router.get('/:partyCode', function(req, res, next){

  Party.getPartyByPartyCode(function(err, party) {
        if (err) {
            return console.error(err);
        } else {
            //respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
            res.format({

              //response in dust or jade files
              html: function(){
                  res.render('Party', {
                        title: 'Party',
                        "party" : party
                    });
              },

              //JSON response will show all parties in JSON format
              json: function(){
                  res.json(party);
              }
          });
        }
  });
});
现在,当我使用ajax时:

var inputElem = $('#partyForm :input[name="partyCode"]'),
    inputVal = inputElem.val(),
    data = { partyCode : inputVal },
    eReport = ''; //error report

$.ajax(
{
    type: "GET",
    url: "/Party",
    dataType: "json",
    data: data,
    beforeSend: function(jqXHR, settings)
    {
        console.log(settings.url);
    },
    success: function(party)
    {
        if (party)
        {
          console.log(party);
          return 'Party ' + party.partyName + ' has already taken party code: ' + party.partyCode + '. Please choose a different PartyCode.';
        }
        else
        {
          console.log("party does not exist.");
           return true;
        }
    },
    error: function(xhr, textStatus, errorThrown)
    {
        alert('ajax loading error... ... '+url + query);
        return false;
    }
});

我的问题是:为什么上面的ajax调用会返回所有参与方?我只想让一方的patyCode被传递到ajax调用的数据中……

路由器响应代码和ajax函数中都有一些错误:

首先更正路由器代码:

您没有在模型中使用提供的参与方代码

router.get('/:partyCode', function (req, res, next) {

var partyCode = req.param('partyCode');

Party.getPartyByPartyCode(partyCode, function (err, party) {
    if (err) {
        return console.error(err);
    } else {
        //respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
        res.format({
            //response in dust or jade files
            html: function () {
                res.render('Party', {
                    title: 'Party',
                    "party": party
                });
            },
            //JSON response will show all parties in JSON format
            json: function () {
                res.json(party);
            }
        });
    }
});
}))

正确的Ajax函数调用

您必须按照路由器的指示,将参与方代码作为URL参数提供,
/:partyCode
。请尝试以下操作:

var inputElem = $('#partyForm :input[name="partyCode"]'),
    inputVal = inputElem.val(),
    eReport = ''; //error report

$.ajax({
    type: "GET",
    url: "/"+inputVal,
    dataType: "json",
    data: data,
    beforeSend: function (jqXHR, settings) {
        console.log(settings.url);
    },
    success: function (party) {
        if (party)
        {
            console.log(party);
            return 'Party ' + party.partyName + ' has already taken party code: ' + party.partyCode + '. Please choose a different PartyCode.';
        }
        else
        {
            console.log("party does not exist.");
            return true;
        }
    },
    error: function (xhr, textStatus, errorThrown) {
        alert('ajax loading error... ... ' + url + query);
        return false;
    }
});

路由器响应代码和ajax函数中都存在一些错误:

首先更正路由器代码:

您没有在模型中使用提供的参与方代码

router.get('/:partyCode', function (req, res, next) {

var partyCode = req.param('partyCode');

Party.getPartyByPartyCode(partyCode, function (err, party) {
    if (err) {
        return console.error(err);
    } else {
        //respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
        res.format({
            //response in dust or jade files
            html: function () {
                res.render('Party', {
                    title: 'Party',
                    "party": party
                });
            },
            //JSON response will show all parties in JSON format
            json: function () {
                res.json(party);
            }
        });
    }
});
}))

正确的Ajax函数调用

您必须按照路由器的指示,将参与方代码作为URL参数提供,
/:partyCode
。请尝试以下操作:

var inputElem = $('#partyForm :input[name="partyCode"]'),
    inputVal = inputElem.val(),
    eReport = ''; //error report

$.ajax({
    type: "GET",
    url: "/"+inputVal,
    dataType: "json",
    data: data,
    beforeSend: function (jqXHR, settings) {
        console.log(settings.url);
    },
    success: function (party) {
        if (party)
        {
            console.log(party);
            return 'Party ' + party.partyName + ' has already taken party code: ' + party.partyCode + '. Please choose a different PartyCode.';
        }
        else
        {
            console.log("party does not exist.");
            return true;
        }
    },
    error: function (xhr, textStatus, errorThrown) {
        alert('ajax loading error... ... ' + url + query);
        return false;
    }
});

您的回答给了我语法错误:url中的+后面有一个额外的逗号。所以,我把它去掉,再试一次。我知道这个解析为找不到的。您的回答给了我语法错误:url中的+后面有一个额外的逗号。所以,我把它去掉,再试一次。我知道这是找不到的解析。您需要将您的party\u id添加到ajax url中,如下-
url:“/party/”+party\u id,
。那么服务器端的匹配路由应该类似于“/Party/:partyCode”这个函数如何工作Party.getPartyPartyCode?我看不到它使用任何partyCode作为输入。@ManishJangir您能告诉我如何将我的party代码传递给该函数吗?您需要像这样将您的party id添加到ajax url-
url:“/party/”+party id,
。那么服务器端的匹配路由应该类似于“/Party/:partyCode”这个函数如何工作Party.getPartyPartyCode?我没有看到它使用任何partyCode作为输入。@ManishJangir您能告诉我如何将我的partyCode传递给该函数吗??