My JavaScript code is barely an Ajax request that expects XML to be returned from back-end. The back-end can return execute_callback
as one of XML tags like this:
<?xml version="1.0" encoding="windows-1251"?>
<response>
<execute_callback>
<function_name>someFunction</function_name>
</execute_callback>
</response>
And everything is okay as far as you know the exact number of parameters this callback
expects. But what if the back-end has returned
<?xml version="1.0" encoding="windows-1251"?>
<response>
<execute_callback>
<function_name>someFunction</function_name>
<param>10.2</param>
<param>some_text</param>
</execute_callback>
<execute_callback>
<function_name>otherFunction</function_name>
<param>{ x: 1, y: 2 }</param>
</execute_callback>
</response>
How do I now pass parameters 10.2 and 'some_text' to someFunction
and JSON { x: 1, y: 2 } to otherFunction
?
I know an ugly solution (using function's arguments
), but I am looking for a pretty one.
And before I forget: don't parse the XML for me - I can do that on my own :) All I need is somewhat of a trick to pass an arbitrary number of arguments to a function in JavaScript. If you know Python, I want:
def somefunc(x, y):
print x, y
args = { 'x' : 1, 'y' : 2 }
somefunc(**args)
but in JavaScript.