0

I am trying to pass a Javascript function as the JSONObject element in primefaces.

In my Java backing bean, I have:

JSONObject serie = new JSONObject();
serie.put("name", "Single data serie");
...
serie.put( <other JSON fields and objects> )
...
serie.put("method", "function() { return '1'; }");

Then I send the object serie to frontend and use that JSONObject in my Javascript. Everyting works fine, except that "method" is being considered as String, not as function.

Is there any way to send the function as function, not String?

I use the

org.primefaces.json.JSONObject

class. I know, that there is also the class

org.json.JSONObject

but it seems to me as the same.

Regards,

Mateusz

1

1 Answer 1

0

Sounds like bad design, but you're probably looking for eval();

Some code sample:

eval("function x() { return '1'; } x();"); 

prints "1".

Sign up to request clarification or add additional context in comments.

2 Comments

Hi. Are you sure, that this will work with the json property? I see here only a replacement of the JSON property value from: "method": "function() { ...}" to "method": "eval(\"function....\")" Literally both properties are Strings. Regards,
Hi again. I worked it out, see my comment to the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.