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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

javascript - Bootstrap Popover: iOS Safari strange behaviour -

spring cloud - How to configure SpringCloud Eureka instance to point to https on non standard port -