简体   繁体   English

从后端发送数据到前端公共 Javascript 文件

[英]Sending data from backend to frontend public Javascript file

I want to send data from my backend to the frontend public javascript file.我想将数据从我的后端发送到前端公共 javascript 文件。 I am aware of using ejs, but I have a dedicated javascript file and I want to send data to that file.我知道使用 ejs,但我有一个专用的 javascript 文件,我想将数据发送到该文件。

Here is my backend nodejs code:这是我的后端 nodejs 代码:

router.get('/', isAuth, function (req, res) {
User.find({ id: req.user[0].id }).populate("tweets").populate("tags").exec(function (err, user) {
    res.render("dashboard", {user: user});
    // res.send(user);
});
});

Here is how I am calling the script file from the dashboard.ejs just before end of body tag:以下是我在 body 标记结束之前从 dashboard.ejs 调用脚本文件的方式:

<script src="/dashboard/scripts/script.js"></script>

And this is where I want the data that I sent from the backend to be stored.这就是我希望存储从后端发送的数据的地方。

const ext = data_from_backend

I have been trying a lot, but not able to found a solution.我一直在尝试很多,但无法找到解决方案。

I want to send data from my backend to the frontend public javascript file.我想将数据从后端发送到前端公共 javascript 文件。 I am aware of using ejs, but I have a dedicated javascript file and I want to send data to that file.我知道使用 ejs,但我有一个专用的 javascript 文件,我想将数据发送到该文件。

Here is my backend nodejs code:这是我的后端nodejs代码:

router.get('/', isAuth, function (req, res) {
User.find({ id: req.user[0].id }).populate("tweets").populate("tags").exec(function (err, user) {
    res.render("dashboard", {user: user});
    // res.send(user);
});
});

Here is how I am calling the script file from the dashboard.ejs just before end of body tag:这是我在 body 标记结束之前从 dashboard.ejs 调用脚本文件的方式:

<script src="/dashboard/scripts/script.js"></script>

And this is where I want the data that I sent from the backend to be stored.这就是我希望存储从后端发送的数据的地方。

const ext = data_from_backend

I have been trying a lot, but not able to found a solution.我一直在尝试很多,但无法找到解决方案。

You can try converting your object to a string and then passing it to the script tag.您可以尝试将 object 转换为字符串,然后将其传递给脚本标签。 Then you can parse that string, convert it to and object and then store it as a global variable.然后您可以解析该字符串,将其转换为 object,然后将其存储为全局变量。 Other script tags should be able to access global variables.其他脚本标签应该能够访问全局变量。

Change your node.js file to:将您的 node.js 文件更改为:

res.render("dashboard", {user: JSON.stringify(user)});

In your.ejs file add a script tag before your <script src="/dashboard/scripts/script.js"></script> :在 your.ejs 文件中,在<script src="/dashboard/scripts/script.js"></script>之前添加一个脚本标签:

<script>
    const str = "<%= name %>";
    const obj = str.replace(/&#34;/gi, '\'');
</script>

Now you can use obj inside other script tags现在您可以在其他脚本标签中使用 obj

<script>alert(obj);</script>

So, I solved the core of the problem but also took help from answers here.所以,我解决了问题的核心,但也从这里的答案中得到了帮助。 Here is what I did.这是我所做的。 I first converted the object to a string using JSON.stringfy() and then sent it to the front-end script and then convert it again using JSON.parse() .我首先使用JSON.stringfy()将 object 转换为字符串,然后将其发送到前端脚本,然后使用JSON.parse()再次转换。 But, I was getting JSON code 'T' error because of '/' (slashes) in my embed code, hence I had to change the embed code to something else that was taken care of in the front-end script.但是,由于嵌入代码中的“/”(斜杠),我收到了JSON code 'T'错误,因此我不得不将嵌入代码更改为前端脚本中处理的其他内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM