0

this is my current problem, so i passed stringified array to client from server and i was trying to get it with following code

const stringifiedArray = "<%= array %>";

and it was returning string but when i was trying to parse it, it was returning JSON error "Unexpected Token in position N" and when i changed double quotes with backquote and equation sign with minus sign: (backquote)<%-array%>(backquote) it worked and parsed perfectly, but what is i wanted escaped string to be parsed? how can i do that?

iLiA
  • 3,053
  • 4
  • 23
  • 45
  • 1
    Is `"<%- JSON.stringify(array) %>"` what you're looking for? – Wyck Sep 05 '19 at 18:29
  • no, i am stringifying array in server side and i am looking for parsing it in client side – iLiA Sep 05 '19 at 18:31
  • Is the code you provided server-side code or client-side code? I would expect on the server you would embed the `JSON.stringify`-ed version into a rendered script tag. And on the client, it just uses the rendered script, no parsing necessary. So I'm not sure what you're trying to do. – Wyck Sep 05 '19 at 18:33
  • 1
    Possible duplicate of [Passing vars from ejs to javascript (server to client on render) while avoiding XSS issues](https://stackoverflow.com/questions/32262022/passing-vars-from-ejs-to-javascript-server-to-client-on-render-while-avoiding) – Wyck Sep 05 '19 at 18:41
  • Try printing that variable out to a comment in your ejs template. For example: `// <%= array %>` We need to see what the actual value you are trying to embed within quotes is. – Jordan Kasper Sep 05 '19 at 20:23

1 Answers1

1

Use

const stringifiedArray = <%- JSON.stringify(array) %>;

instead of

const stringifiedArray = "<%= array %>";

and do not stringify your object or array at server side