transfer data from jade to jquery and vice versa

2019-07-28 03:36发布

问题:

My index.js file:

res.render('index', {data:{'hello':'world'}});

my jade file:

p #{data}
script(src="/javascripts/app.js")

This prints the value json object.

now on my app.js file console.log(data); gives an error saying data is undefined.

How can I access the data which was passed from my index.js file in my javascript file.

回答1:

In your index.js file:

res.render('index', {data:JSON.stringify({'hello':'world'})});

in your Jade Template:

script(type="text/javascript").
  var data = !{data};

That'll give you an object in the client-side JavaScript.