gpt4 book ai didi

javascript - 如何使用 request.query 检索变量

转载 作者:行者123 更新时间:2023-11-28 19:56:24 27 4
gpt4 key购买 nike

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

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

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

getContents();

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

最佳答案

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

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

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

关于javascript - 如何使用 request.query 检索变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22469073/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com