简体   繁体   English

从服务器端获取客户端变量(express.js,node.js)

[英]Get variable on client-side from the server-side (express.js, node.js)

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? 如何从服务器端获取客户端变量'variable'?

I tried: 我试过了:

alert(variable);

You can't get server-side variable in a node application in client-side javascript in a browser window directly. 您无法直接在浏览器窗口中的客户端javascript中的节点应用程序中获取服务器端变量。 Although they support the same programming language, they're just two different runtimes. 虽然它们支持相同的编程语言,但它们只是两个不同的运行时。

your question is about how client side javascript can communicate server-side resources like /auth . 您的问题是关于客户端javascript如何与/auth类的服务器端资源进行通信。 options are: 选项是:

Provide your data in script tag on your web page rendered by jade template. 在由jade模板呈现的网页上的脚本标记中提供您的数据。 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. 然后你可以使用alert(generatedData)来获取它。 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 communicate with server-side resource. 通常人们使用JSON,这意味着您需要编写一些客户端代码来与服务器端资源进行通信。 like using jQuery in client side: 比如在客户端使用jQuery:

$.get('/auth').done(function(data){ alert(data); });

With server-side code where it sends data in JSON by express response object automatically: 使用服务器端代码,它通过快速响应对象自动发送JSON数据:

app.get('/auth', function(req, res) {
    res.send({
        variable: true
    });
});

The object {variable:true} will only be usable in auth.jade when jade compiles it. 对象{variable:true}只有在jade编译时才能在auth.jade中使用。 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. 您可以将它添加到隐藏元素并使用客户端javascript读取它,或者更好,从ajax-call获取它作为json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从node.js(服务器端)获取数组以响应app.js(客户端) - Get array from node.js (server-side)to react app.js (client-side) 如何在Angular / Node.js / Express中将客户端参数传递给服务器端 - How to pass client-side parameters to the server-side in Angular/Node.js/Express 获取客户端javascript代码中可用的node.js服务器端对象 - Get node.js server-side object available in client-side javascript code 从客户端(Node.js)的服务器端调用函数 - Calling a function from server-side on the client-side (Node.js) 将服务器端变量发送到 node.js 中的客户端 JS - Sending server-side variables to client-side JS in node.js 在express.js / node.js中包含标头+自定义服务器端.js的正确方法? - Proper way to include headers + custom server-side .js in express.js / node.js? Node.js:客户端模板v / s服务器端模板 - Node.js: Client-Side Templating v/s Server-Side Templating Node.js服务器端表单验证与客户端响应 - Node.js server-side form validation with client-side response 使用Node.js实时:WebSocket +服务器端轮询与客户端端轮询 - Real-Time with Node.js: WebSocket + Server-Side Polling vs. Client-Side Polling 客户端JS&服务器端C#通信 - Client-side JS & Server-side C# Communication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM