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

时间:2016-07-08 10:34:08

标签: javascript jquery ajax node.js

我是Web开发的新手。我曾经使用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);
  });
};

现在,我还有一个名为Party.js的路由,其中​​我有两个get方法,如下所示:

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调用的数据....

1 个答案:

答案 0 :(得分:2)

路由器响应代码和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;
    }
});