What is the purpose of using eval in this manner (and this would be my
usage - data returned from an XMLHttpRequest).
var data = eval('(' + req.responseText + ')');
Why the inner parenthesis?
TIA
--
*****************************
Chuck Anderson • Boulder, CO
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************
>
> var data = eval('(' + req.responseText + ')');
>
In JavaScript, "{" and "}" are used to denote
- a block of code and
- an object literal
when you execute eval( "{'key':value}" ), "{" is executed as a block
of code instead of an object literal. Hence inner parenthesis is used
to force eval to treat the json-string as an object literal.
You can go through this for detailed explanation:
http://rayfd.wordpress.com/2007/03/28/why-wont-eval-eval-my-json-or-json-object-object-literal/
Kiran Makam
<quote>
Note something about this parser though: it is REALLY strict. It won’t
parse perfectly OK object literals like { name: “Ray”, age: 31 }. Eval
parses this without problems:
</quote>
It's a common error to build JSON texts without the required enclosing
quotes in the object's properties' names, but such not-really-JSON
texts won't pass the eval() when/if any of the properties' names are
=== to a JS reserved word :
eval('( { do: 5, while: 10 } )'); -> err
eval('( { "do": 5, "while": 10 } )'); -> ok
or when/if the properties' names just somehow look awful to the JS
parser:
eval('( { : 5, *: 6, how many: 7 } )'); -> err
eval('( { "": 5, "*":6, "how many": 7 } )'); -> ok
IMO, this things ought to be in the FAQ:
http://www.google.com/search?&q=JSON+site:jibbering.com/faq/
"Your search - JSON site:jibbering.com/faq/ - did not match any
documents."
: pitiful.
--
Jorge.
Ahhhhh ... syntactic ambiguity. Why didn't you just say so? ��Ԭ
No wonder I couldn't figure it out for myself.
So .... while I'm on this topic. I'm sending data back to an
XMLHttpRequest from Php and not even using JSON. I merely use:
<?php
echo "var somevar1 = 'somevalue1';";
echo "var somevar2 = 'somevalue2';";
exit;
?>
Then I just "eval(req.responseText);"
Is there any need for me to convert this to an object and use JSON
instead when simply passing back a group of simple variables?
<?php
$json = array()
$json['somevar1'] = 'somevalue1';
$json['somevar2'] = 'somevalue2';
$json_encoded = json_encode($json);
echo $json;
exit;
?>
--
*****************************
Chuck Anderson � Boulder, CO
Yes there is a good reason to use JSON rather than arbitrary
JavaScript: non-browser clients. JSON is a data interchange format and
libraries to read and write JSON exist in many languages. If a non-
browser client is consuming your data in JSON format then the client
only needs a JSON library. If your server sends back arbitrary
JavaScript then either ad-hoc parsing or a full JavaScript parser
would be necessary and either of these is an ugly solution.
Transporting data in a data format, like JSON, for flexibility is a
good idea.
Peter
That ought to be in the FAQ under:
http://jibbering.com/faq/#eval
with a link to http://json.org/
Garrett
--
comp.lang.javascript FAQ: http://jibbering.com/faq/
That's where I first went to look for my answer.
--
*****************************
Chuck Anderson � Boulder, CO
<FAQENTRY>
Peter
Yes, duly noted! I actually already added that in the draft on my local
machine.
BTW - Tagging FAQ**TRY can help, but I don't search "<FAQENTRY>" that
often.
> Peter