I have a json data in controller file i got this data from API so i can able to redirect this data to ejs file but when i write the code in body part like this <%-JSON.stringify(jsonData)%> it will be display the entire json but when i am using this statement under script tags i can't able get anything [object object] error message is came.
How can i use this json data under script tags for displaying each and every filed from json data? Can you suggest anyone?
In Controller:
res.render('display', {
jsonData: storeJSONData
});
I written this code for redirecting template.
Please try this in your HTML and it should work:
<script type="text/javascript">
function codeAddress(){
var data = <%- jsonData %>;
document.write(JSON.stringify(data));
}
</script>
This is because you can't use JS inside the EJS tags, you need to render it to be assigned to a var and then you can play with it.
Try changing your render() to:
res.render('display', {
jsonData: JSON.stringify(storeJSONData)
});
and your template to:
<script type="text/javascript">
function codeAddress() {
var data = <%- jsonData %>;
document.write(data);
}
</script>