views:

24

answers:

2

I have json data being returned 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?

A: 

Yes, yes I do. Thanks, me! Hopefully this will help someone else as well.

b. e. hollenbeck
+2  A: 

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.

bcherry