admin管理员组文章数量:1315347
Server-side:
app.get('/auth', function(req, res) {
res.render('auth.jade', {
variable: true
});
});
How to get variable 'variable' on client-side from the server side?
I tried:
alert(variable);
Server-side:
app.get('/auth', function(req, res) {
res.render('auth.jade', {
variable: true
});
});
How to get variable 'variable' on client-side from the server side?
I tried:
alert(variable);
Share
Improve this question
asked Apr 12, 2014 at 13:43
owlowl
4,5014 gold badges26 silver badges28 bronze badges
2 Answers
Reset to default 7You can't get server-side variable in a node application in client-side javascript in a browser window directly. Although they support the same programming language, they're just two different runtimes.
your question is about how client side javascript can municate server-side resources like /auth
. options are:
Provide your data in script tag on your web page rendered by jade template. for example:
html(lang="en")
head
title= pageTitle
script(type='text/javascript').
var generatedData = {variable:true}
body
Then you can use alert(generatedData)
to get it. notice that the data has to be serializable data without any function or reference.
Usually people use JSON which means you need to write some client side code to municate with server-side resource. like using jQuery in client side:
$.get('/auth').done(function(data){ alert(data); });
With server-side code where it sends data in JSON by express response object automatically:
app.get('/auth', function(req, res) {
res.send({
variable: true
});
});
The object {variable:true}
will only be usable in auth.jade when jade piles it. You can add it to a hidden element and read it using client-side javascript, or perhaps better, get it as json from an ajax-call.
本文标签: javascriptGet variable on clientside from the serverside (expressjsnodejs)Stack Overflow
版权声明:本文标题:javascript - Get variable on client-side from the server-side (express.js, node.js) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741976330a2408146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论