I have a callback function within an ASP.NET Web Forms application which I am trying to return some handy JSON to so that the javascript function manage the data. I am able to send back a string of JSON that contains all of my data, but now I am trying to get the following structure:
{
data: dataGoesHere
function: function(){alert('hello');}
}
jQuery.parseJSON works great for the simple case of a string being returned like this:
"{\"data\" : \"dataGoesHere\"}"
but soon as you try to do something like this
"{
\"data\" : \"dataGoesHere\"
\"function" function(){alert('hello');}
}"
jQuery throws an exception. I am wondering if it is possible to create a string so that jQuery can parse into JSON that will include a function reference that I can call later.
I hope this makes sense. Thank you for your time and responses!
That’s not valid JSON.
It’s rejected by jQuery for security reasons, in order to prevent exactly what you’re trying to do.
Instead, you can just call
eval, to interpret it as a Javascript literal.Note that you’ll need to wrap it in parentheses to make sure that it’s parsed as an expression and not a block.