Tuesday, April 24, 2007

teamsite: passing parameters from javascript to perl

Teamsite (the evil abomination called software it is) allows the unfortunate souls using it to call perl scripts for javascript and pass parameters to the perl script.

the syntax looks something like this:

var params = new Object();
params.val = "some string";
var server = window.location.hostname;
IWDatacapture.callServer("http://"+server+"/iw-bin/somescript.ipl",params);

problem:
sometimes the params dont carry over correctly from javascript to the perl script, ie, all params have values in the javascript, but on the perl script side, they have no values - ""

the problem manifests if you do this:
when you assign the params in this way:
var params = new Object();
params.val = SomeFunctionWhichReturnsAString();
now chances are that params.val = "" in the perl script.

solution:
this problem stinks of pointers. params.val is set given a reference to something in the javascript heap, when perl takes over, that references points to nothing. Either way, this is how you get around it:
var params = new Object();
params.val = SomeFunctionWhichReturnsAString()+"";

now everything works. why? well... because teamsite is evil.

2 comments:

  1. Hello Mikhail,

    What if we are passing int value?

    Thanks,
    Mahendra

    ReplyDelete
  2. @mahendra: i posted this 4 years ago, but ill take an educated guess and say that you must pass the int as a string, since IWDatacapture.callServer() must do a GET or a POST to call the server, and in both of those cases, any params passed will be strings. its up to perl to figure out what to do with them.

    so pass "3" instead of 3

    ReplyDelete