$.each() iterating over characters in a string - do I need to eval()?
I have json data being retu开发者_JS百科rned as a collection:
var foo = ["6", "7", "33"]
using JSONP in jQuery. Since I'm using JSONP, the data is being returned to a callback function, which is interpreting it as a string instead of a collection. Do I need to run eval(foo)
on the string in the callback before handling it as a collection, or is there some other means of recasting it?
If you're using jQuery, you should be doing .ajax
with dataType: "jsonp"
, which should send the parsed data to your callback, rather than a string. If this doesn't work for you, or there are other complexities I'm not seeing, you can use $.parseJSON(foo)
instead of eval(foo)
. This will call the browser's native JSON.parse
method if it exists, or use eval
if it doesn't.
精彩评论