0

I currently have an ajax custom control that makes calls to a web service as part of its functionality. I need to pass which web service to call to the control as a parameter. Currently, I'm using a string parameter in the .vb file and passing it to the javascript. I execute the calls to the web service by building a line as a string, then using eval() on it.

From here, I can see that eval is bad. Like the function calls on that page, is there any way that I can use the web service as something that I can call?

Note, I am calling methods from the web service. Instead of stringservice(parameters), it is stringservice.start(parameters). And I just get the error that strings don't have a start method.

Any solution, or am I stuck using eval? Thanks :)

Community
  • 1
  • 1
Ryan Endacott
  • 8,772
  • 4
  • 27
  • 39
  • Please show us the codes that build the function string. – Bergi Jul 23 '12 at 14:00
  • Currently it's just like this: `var starter = this.WebService + '.Begin(this.ProcessID, this.ProcessName, params);'; eval(beginProcess);` But it seems that I should be using JSON format? I'll look into that. – Ryan Endacott Jul 23 '12 at 14:20

2 Answers2

1

If your string is in the JSON format, you can safely convert it using JSON.parse

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
1

You can access properties of the global object with the bracket syntax as well:

window[this.WebService](this.ProcessID, this.ProcessName, params);

However, I'd recommend to bundle the available functions in an object of webservices, like

var webservices = {
    a: function(id, name, params) { ... },
    b: function(id, name, params) { ... },
    ...
};

...

webservices[this.WebService](this.ProcessID, this.ProcessName, params);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Will this method still work for method calls? Like, can I use `webservices[this.WebService].Begin();`? Or would it work if I put the begin within the webservice. Ex: `this.WebService = 'testservice.begin'`? Thanks for your help :) – Ryan Endacott Jul 23 '12 at 14:48
  • Yes, when a property points to an object you can access that ones properties with dot syntax again. The second one won't work, this is exactly the case for property names containing invalid identifier characters like dots, brackets etc. – Bergi Jul 23 '12 at 15:49