Wednesday, March 21, 2012

Use dynamic parameters when calling web service from javascript

Hello,

I would like to call a web services from javascript with dynamic parameters. I don't know the count and the value of the params that I'll set.

Here is a part of my code :

var serviceMethod =new Sys.Net.ServiceMethod(_servicePath, _serviceMethod, _appUrl);var paramArray =new Array(_serviceMethodParameters.length);for(var i = 0 ; i < _serviceMethodParameters.length ; i++ )

{

var toto = _serviceMethodParameters[i].get_Key();var tata = _serviceMethodParameters[i].get_Value();var elem = { toto : tata };

paramArray.push(elem);

}

_request = serviceMethod.invoke(paramArray, onMethodComplete, onMethodTimeout, onMethodError, onMethodAborted,

this , _timeoutInterval, _priority);

the problem that I have is actually I don't figure out the data format of the parameters

I would like the variable toto to be not interpreted as "toto", but as the content of the variable toto.

I could do this in another way, but I have to use this method to call a web service because I need to abort it if needed.

Any ideas about that?

Thanks in advance

Would something like this do the trick?

var elem = {};
elem[toto] = tata;
paramArray.push(elem);

Hope that helps,
-Hao


yes, it's working now, but now i've another problem : the parameter format is not correct.

When the webservice is called, it returns an error which is :

"error: Invalid object passed in, '{' expected. (1): [{\"last\":\"5000\"}]"


I actullay got it !! :)

I didn't need to use an Array to set the parameter but only a variable, kind of hashtable

I write the code if it could help someone who has the same problem.

here is the code :

var serviceMethod =new Sys.Net.ServiceMethod(_servicePath, _serviceMethod, _appUrl);var elem = {};for(var i = 0 ; i < _serviceMethodParameters.length ; i++ )

{

var toto = _serviceMethodParameters[i].get_Key();var tata = _serviceMethodParameters[i].get_Value();

elem[toto] = tata;

}

_request = serviceMethod.invoke(elem, onMethodComplete, onMethodTimeout, onMethodError, onMethodAborted,

this ,_timeoutInterval, _priority);

No comments:

Post a Comment