简体   繁体   English

JavaScript中的动态值-Node.js

[英]Dynamic values in javascript - Node.js

In node.js i am sending the values to html through 在node.js中,我通过以下方式将值发送到html:

render('/abc', mydata)

"mydata" contains json_encoded format. “ mydata”包含json_encoded格式。

And i am reading this as : 我读这是:

{{=mydata}}

Now, in the same html page i have javascript like : 现在,在同一个html页面中,我有如下javascript:

 <script>
 xyz(); 
  function xyz() { 
      // i need to read the "mydata" here.
  }
 </script>

I tried this which didn't work xyz({{=mydata}}) , How can i use that dynamic data in node.js ?? 我尝试了这个不起作用的方法xyz({{=mydata}}) ,如何在node.js中使用该动态数据?

A few possibilities: 一些可能性:

  • mydata may not actually be a local in the view. mydata在视图中可能实际上不是本地的。

    Express can't pass along the name of the variable used as the argument. Express无法传递用作参数的变量名称。 Only the properties of the Object it references. 仅它引用的Object的属性。

    So, you may need to create a new Object around mydata to name a property for it: 因此,您可能需要围绕mydata创建一个新的Object为其命名一个属性:

     render('/abc', { mydata: mydata }); 
  • The output may be HTML-encoded by {{= }} , which will likely cause SyntaxErrors in JavaScript. 输出可能是由{{= }}进行HTML编码的,这很可能会导致JavaScript中的SyntaxErrors。

    So, the response may contain something like: 因此,响应可能包含以下内容:

     xyz({&quot;foo&quot;:&quot;bar&quot;}) 

    Rather than: 而不是:

     xyz({"foo":"bar"}) 

    How to go about skipping HTML-encoding will depend on which view engine you're using with Express. 如何跳过HTML编码将取决于您在Express中使用的视图引擎。 But, it may be as simple as replacing the = with a - : 但是,就像将=替换为-一样简单。

     xyz({{-mydata}}) 
  • mydata may still be an Object rather than the String of json_encoded data it seems you were expecting. mydata可能仍然是一个Object而不是您期望的json_encoded数据的String

    If that's the case, it may be using the standard .toString() , which will produce: 如果是这种情况,则可能使用的是标准.toString() ,它将产生:

     xyz([object Object]) 

    And, you may still need to stringify() mydata . 并且,您可能仍然需要stringify() mydata

     xyz({{-JSON.stringify(mydata)}}) 

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

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