将参数传递给节点.js中的路由
Passing parameters to routes in 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调用数据的一方......
路由器响应代码和 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;
}
});
相关文章:
- 使用Ampersand.js路由模块化应用程序
- 在node-js路由中发出http请求
- 更改路由后,运行一次js脚本
- Angular JS-如何强制浏览器记录历史以便快速路由
- 从Sencha Touch迁移到EXT JS 6-Contoller的默认/空路由
- 尝试使用Node.js动态路由从IMDB中抓取电影内容.但是在我的output.json文件中没有定义
- ember.js:转换到相同的路由会产生堆栈溢出问题
- Angular JS和Node路由/布线-仅在页面刷新后显示数据
- express js中具有不同功能的动态路由
- Sails.js在v1、v2等中分组路由
- 如何将Sails.js挂钩路由连接到策略
- 不更改路由位置backline.js的触发器方法
- angular js路由不起作用
- Hapi-js重定向到另一个带有额外请求数据的路由
- 如何防止在主干网中路由.js
- 骨干网中的高级路由.js具有功能分组
- 我如何使用外部路由.js,这样我就不必在app.js中定义我的路由了
- 带页面的聚合物路由.js在新选项卡中打开链接时,不要向 URL 添加哈希标记
- Heroku错误:页面是通过HTTPS加载的,但请求了一个不安全的脚本“角度路由.js”
- ngRoute 依赖注入错误,但加载了角度路由.js.min