Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a difference between single/double quoted strings passed to javascript's eval?
    primarykey
    data
    text
    <p>i have a server message sent via web-sockets. that message is a json (validated) string. when it gets to the browser i check that it is a string with <code>typeof(data)</code> and it tells me that it is, in fact, a string. When finally i do <code>var some_obj = eval( '(' + data + ')' );</code> it gives me an <code>Uncaught SyntaxError: Unexpected token ILLEGAL</code> error.</p> <p>also, before using <code>eval()</code>, i <code>console.log(data)</code> and it displays correctly, although an <code>alert(data)</code> won't show anything on the dialog.</p> <p>i can't understand what's happening.</p> <p>i also tried <code>var myJson = '{ "x": "Hello, World!", "y": [1, 2, 3] }';</code> and then <code>var myObj = eval( '(' + myJson + ')' );</code> and it works, so i really can't understand why mine can't be evaluated (parsed).</p> <p>the string received via web-sockets is this:</p> <pre><code>received 37 bytes » { "cmd": "setname", "params": "ok" } </code></pre> <p>where <code>data = { "cmd": "setname", "params": "ok" }</code> (with quotes i suppose, because of <code>typeof(data)</code> being = string).</p> <p>any tips? thanks</p> <p>edit1 » with web-sockets, you have to prepend a null char (0 ascii) and append a escape char (255 ascii) to the output string from the server. i assume the client (browser) as it implements web-sockets must deal with this and unwrap the string correctly (as the standard) and as i do in my server. thing is, there might be some escape char left and it doesn't deal with it correctly. but the problem only started when i tried to send json strings to be eval()ed. otherwise they work properly as any other string.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload