How to render JSON in Rails with JS function in field? -
i want give response server this:
{foo: "some value", bar: function(){console.log(this);}}
but if write response line in controller so:
render json: {foo: "some value", bar: 'function(){console.log(this);}'}
the result follows:
{foo: "some value", bar:"function(){console.log(this);}"}
don't use eval — it's unsafe , tricky. wrap function body quotes, remove keyword function out of there , create new function on clientside function constructor.
json:
{ "fn": "alert(arguments);" }
clientside parsing code:
var myfunction = new function(json.parse(jsonstring).fn);
this better because function won't automatically execute code or expose global context.
the considerable inconvinience absence of arguments list. have use arguments
object instead.
Comments
Post a Comment