What's the equivalent to making this call eval('('+responseText+')') in jQuery? I also understand that eval is not that safe, so I'm looking for something safer and more idiomatic in jQuery.
Are you actually looking to execute Javascript code (which will always be unsafe to some extent), or are you trying to parse JSON from a string into an object?
If the former, eval() is still going to be your best bet.
If the latter, jQuery will do it for you as part of an AJAX request, or you can use a dedicated JSON plug-in to convert between string and object without the risk of executing arbitrary code.
a) To load and execute JS code:
$.getScript( url, [ success(data, textStatus) ] )
see http://api.jquery.com/jQuery.getScript/
b) To parse JSON:
$.parseJSON( json )
see http://api.jquery.com/jQuery.parseJSON/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With