【问题标题】:How can I use request.query to get a variable如何使用 request.query 获取变量
【发布时间】:2014-03-18 00:10:57
【问题描述】:

在我的客户端代码中,我调用了 getContents():

$.getJSon("/getContents", function(room){
  theRoom=$("#roomName").val();//textarea's value
...
});

在 getContents() 中,它位于服务器端代码 (index.js) 中,我如何使用 request.query 或任何其他函数来获取 theRoom(variable) 以便我可以获取基于 Room 的内容在它的标题(房间)上?

getContents();

var getContents=function(req,res){
...
var room=?
...
roomCollection.findOne({name: room}, function(err,theRoom){...
  res.send(theRoom)
});
...
}

【问题讨论】:

标签: javascript jquery web-applications express


【解决方案1】:

根据您的 JSON 的结果,以下可能是一个好的开始:

$.getJSON("/getContents", function(data){
  var theRoom=$("#roomName").val();//textarea's value
  $.each(data.rooms, function( key, room) {
    if(room.title == theRoom)
       alert(room); 
  });
});

【讨论】:

  • getContents() 发送一个房间而不是整个集合。问题是如何发送与 theRoom 同名的房间。我将更新问题以显示 getContents()
  • 您可以将请求的房间名称发送到服务器并进行一些服务器端查询
  • 我该怎么做?我是 Web 应用程序的新手
猜你喜欢
  • 2014-04-23
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多