Which is equivalent, but more idiomatic for eval () in jQuery?

What is equivalent to making this call eval('('+responseText+')')in jQuery? I also understand that it is evalnot so safe, so I am looking for something more secure and more idiomatic in jQuery.

+3
source share
5 answers

Do you really want to execute Javascript code (which will be unsafe to some extent), or are you trying to parse JSON from a string into an object?

If the first, eval () will still be your best bet.

, jQuery AJAX, JSON plug-in .

+6

a) JS-:

$.getScript( url, [ success(data, textStatus) ] )

. http://api.jquery.com/jQuery.getScript/

b) JSON:

$.parseJSON( json )

. http://api.jquery.com/jQuery.parseJSON/

+4

, . . , , , Firefox undefined, IE6 TypeError.

+2

By design, jQuery intends to improve browser implementation. Since there is no problem with how various browsers implement it eval, it is not processed by jQuery.

+1
source

$.globalEval()?

http://api.jquery.com/jQuery.globalEval/

The documentation says that it behaves very differently than eval(), but its closest match is if you want to pass your grades through jQuery.

0
source

Source: https://habr.com/ru/post/1703922/


All Articles