如何使用节点js express呈现视图模板 [英] How to render view template using node js express
问题描述
function verify(())()键
$ .ajax({
async:true,
type:'GET',
url:'/ zonepass /'+ key,
data:{
'zone_key':key
},
success:function(res){
// alert('发送文本消息到'+ res' ;
}
});
$ b $ p
$ b我在服务器端处理传入数据的Ajax请求查询我的Firebase数据库以获取其他相关信息。
然后尝试使用 res.render('verify',{zone:obj,key :zone_key})
其中verify是另一个 .ejs
文件,我要导航到用户,而JSON对象是我想要的数据传递给该模板。
我的代码不是呈现视图页,我不知道为什么。我控制台记录在服务器上的所有数据,所有的数据被正确的拉动,但是我的视图页面永远不会导航到验证ejs文件...
app.get('/ zonepass /:id',function(req,res){
var zone_key = req.param('zone_key');
var zone_obj = firebase。 ($ snap)=> {
obj = snap.val();
res。 render('verify',{zone:obj,key:zone_key});
});
});
解决方案 res.render ajax
请求无效,ajax调用的响应将在 success
函数,但是 res.render
也不起作用 res.redirect
不能和 ajax一起工作所以你需要使用一个表单或前端重定向到该路由,这在技术上也是<$ c 请求。
使用仅HTML:请求但没有 ajax
例如: / p>
< a href =/ zonepass /< your id>?zone_key =< your zone key>> Verify< ; / A>
使用javascript:
function verify(key){
window.location.href =/ zonepass /+< your id> +?zone_key =<你的区域密钥>
$ b $ p $也可以在你的NodeJS路由中访问 id
使用 req.params.id
和 zone_key
使用 req.query .zone_key
,所以你的服务器代码将是:
$ p $ app $ get('/ zonepass / id',function(req,res){
var id = req.params.id;
var zone_key = req.query.zone_key;
var zone_obj = firebase.database ()。('snap')> {
obj = snap.val();
res.render().ref('zones')。child(zone_key).once('value')。 ('verify',{zone:obj,key:zone_key});
});
});
顺便说一句,你需要在验证视图内部处理,如果密钥没有被验证,验证视图中的错误或消息,密钥不正确...或任何与您的逻辑相关的消息
I create an Ajax request (below) which passes data to the server with the information I need.
function verify(key) {
$.ajax({
async: true,
type: 'GET',
url: '/zonepass/'+key,
data: {
'zone_key': key
},
success: function(res){
//alert('Sent a text message successfully to ' + res);
}
});
}
I handle the Ajax request on the server side where I use the passed in data to query my Firebase DB to get other relevant information.
I then try to render the view page that I want to navigate to using res.render('verify',{zone: obj, key: zone_key})
where verify is another .ejs
file that I want to navigate the user to and the JSON object is the data that I want to pass to that template.
My code is not rendering the view page and I'm not sure why. I console logged all the data on the server and all the data is being pulled properly but then my view page never navigates to the verify ejs file...
app.get('/zonepass/:id', function(req,res) {
var zone_key = req.param('zone_key');
var zone_obj = firebase.database().ref('zones').child(zone_key).once('value').then((snap) => {
obj = snap.val();
res.render('verify',{zone: obj, key: zone_key});
});
});
解决方案 res.render
will not work with an ajax
request, response from ajax call is returned and accessible inside the success
function, but res.render
will not work also res.redirect
will not work with ajax
request.
So you need to submit your request using a form or redirecting on frontend to that route, which is technically also a get
request but without ajax
example:
Using only HTML:
<a href="/zonepass/<your id>?zone_key=<your zone key>">Verify</a>
Using javascript:
function verify(key) {
window.location.href= "/zonepass/"+ <your id> + "?zone_key=<your zone key>"
}
Also in your NodeJS route you can access id
using req.params.id
and zone_key
using req.query.zone_key
, so your server code will be:
app.get('/zonepass/:id', function(req,res) {
var id = req.params.id;
var zone_key = req.query.zone_key;
var zone_obj = firebase.database().ref('zones').child(zone_key).once('value').then((snap) => {
obj = snap.val();
res.render('verify',{zone: obj, key: zone_key});
});
});
BTW you will need to handle inside the verify view, if the key is not verified, example you show an error or message in verify view, that the key is not correct ... or any message related to your logic
这篇关于如何使用节点js express呈现视图模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文