javascript - Not able to parse json string in ejs becuase of single quote in value -
i passing 1 json string variable nodejs ejs.
e.g stringjson = "{"obj" : "it's not working"}"
i retrieving value on ejs inside script tag using var stringifiedjson = '<%- stringjson%>'
so when page being rendered giving error "syntaxerror: missing ; before statement" because stringjson value contain single quote.
i came across solution can replace single quote "\'" problem in solution passing many jsonstring variables node ejs. have same in variables.
i seen in solution below
<script> var stringifiedjson = <%- stringjson%> </script>
without surrounding ' '. showing error "expression require" error.
is there other way can parse jsonstring json object?
something work:
<p>stringjson.obj: <%= stringjson.obj %></p>
i think error come server response, try this:
res.render('index.ejs', { stringjson: {"obj": "it's not working"} });
Comments
Post a Comment