【问题标题】:How to retrieve a variable using request.query如何使用 request.query 检索变量
【发布时间】:2014-04-23 12:38:00
【问题描述】:

在我的客户端代码中,我调用了 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


    【解决方案1】:

    您可以将其作为请求参数传递给$.getJSON() 使用

    $.getJSON("/getContents", {
        room: $("#roomName").val()
    }, function (room) {
        //rest of code
    });
    

    注意:不确定读取请求参数的服务器端语法,请参阅this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2023-02-09
      • 2020-05-26
      • 1970-01-01
      相关资源
      最近更新 更多