如何使用request.query检索变量 [英] How to retrieve a variable using request.query

查看:525
本文介绍了如何使用request.query检索变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my client-side code, I called getContents():

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

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

In getContents(), which is in the server-side code(index.js), how can I use request.query or any other function to get theRoom(variable) so that I can get the contents of a Room based on its title(theRoom)?

getContents();

getContents();

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

推荐答案

您可以将其作为请求参数传递给

You can pass it as a request param to $.getJSON() using

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

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

Note: not sure about the serverside syntax to read the request parameter, see this answer

这篇关于如何使用request.query检索变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆